For any given group of tests, you can set certain pieces of code to run before or after all the tests. This can be useful for setting up databases, cleaning up some state after the tests, or generally, for setting up or tearing down some state that you need in order to run the test itself.
In this next example, the function named runBefore
runs before any of the described tests:
describe('some feature', function() {
before(function runBefore() {
console.log('running before function...'); });
it('should do A', function() {
console.log('test A');
});
it('should do B', function() {
console.log('test B');
});
});
Save this file code into a file named test.js
and install Mocha locally:
$ npm install mocha
Run the tests:
$ node_modules/.bin/mocha test.js
It should give you the following output:
running before function... test A .test B . ✔ 2 tests complete (6ms)
In a similar way, you can also specify a function to be executed after all the...