Big props to andydlindsay @ LHL Vancouver for parts of these notes/demo ✊
- Modules &
npm
, a ❤️ story - Practicing unit testing using a Test-Driven Development (TDD) methodology
- Using the Mocha BDD test framework
- Using the Chai assertion library
- Creating and consuming modules using Node's default CommonJS sytax (
module.exports
andrequire
)
- Test Driven Development
- Tests are written before the code is written
- We use a process called Red-Green-Refactor
- Red: The test is failing
- Green: The test is passing
- Refactor: Improve the existing code safe in the knowledge that the functionality is testable
- BDD: Behaviour Driven Development
- Very similar to TDD, but the focus is more on the end user rather than the quality of the code under test
- A testing framework
- By default, looks in the
test
folder for test files to run
- An assertion library
- We can use assertions to describe the desired outcome of our code
- Chai offers three different interfaces
should
,expect
, andassert
- For most uses, choosing which interface to use is a matter of preference
// should
myVar.should.be.a("string");
myVar.should.equal("hello world");
// expect
expect(myVar).to.be.a("string");
expect(myVar).to.equal("hello world");
// assert
assert.typeOf(myVar, "string");
assert.equal(myVar, "hello world");
- We can export functions, objects, etc from a file using the
module.exports
object - You can add new keys to the
module.exports
object or you can completely overwrite it with a function/object
// adding new keys to module.exports
module.exports.myFunc = myFunc;
module.exports.myOtherFunc = myOtherFunc;
// overwriting module.exports object
module.exports = myFunc;
// or
module.exports = {
myFunc,
myOtherFunc
};
- As you have previously seen, we can bring functions, objects, etc into a file using
require
// basic require syntax
const myFunc = require("./myFunc");
// if the file exports an object, we can use ES6 destructuring
const { myFunc } = require("./myFunc");