Before we start writing the tests for our application, there's a small amount of overhead that we will need to take care of to prepare for our tests. To take care of this overhead, we will write a testhelper file that will be included and run with every test file we execute via Mocha.
Create a file named testhelper.js within the tests folder, and insert the following block of code:
const chai = require('chai'),
sinon = require('sinon'),
sinonChai = require('sinon-chai');
global.expect = chai.expect;
global.sinon = sinon;
chai.use(sinonChai);
This is code that we would typically need to include at the top of every one of our test files; however, by including it in a single file, we can instruct Mocha to automatically require this file for every test file that is run. The file itself just includes the chai and sinon modules and defines a few global variables as shortcuts for our test writing. Additionally, it instructs chai to use the sinonChai module so that our syntax is extended and we can write Sinon-specific Chai assertions. The command to actually run our suite of tests is as follows:
$ mocha -r tests/testhelper.js -R spec tests/**/*.test.js
Based on the path to our tests in the preceding command, it's assumed that the command will be executed from the root of the application project folder. The -r flag instructs Mocha to require the testhelper.js module. The -R flag is an option to define the style of the test reporting output. We chose to use the spec style, which lists our report in a nested indentation style with each describe and it statement, along with a green checkmark for the passed tests. Finally, the last argument is the path to our test files; in this case, we provided wildcards so that all of our tests will run.
Let's write a quick sample test to make sure that our project is properly set up. Within the tests folder, create a new file named mocha.test.js and include the following code:
describe('Mocha', ()=>{
'use strict';
beforeEach(()=>{});
describe('First Test', ()=>{
it('should assert 1 equals 1', ()=>{
expect(1).to.eql(1);
});
});
});
The preceding test is pretty straightforward and simply asserts that 1 is equal to 1. Save this file and run the Mocha test command again, and you should get the following output:
$ mocha -r tests/testhelper.js -R spec tests/mocha.test.js
Mocha
First Test
should assert 1 equals 1
1 passing (5ms)
You might find it tiresome and frustrating to remember and execute that long, convoluted command for Mocha. Fortunately, there's a pretty easy solution. Edit the package.json file in the application, and add the following section:
"scripts": {
"start": "node server.js",
"test": "mocha -r tests/testhelper.js -R spec
tests/**/*.test.js"
},
By making this tweak in the package.json file, you can now simply execute npm test from a command line as a quick and easy shortcut. This is a standard convention with the package.json file, so any developer will know how to simply execute npm test:
$ npm test
> [email protected] test /Users/jasonk/repos/nodebook/chapter9
> mocha -r tests/testhelper.js -R spec tests/**/*.test.js
Mocha
First Test
should assert 1 equals 1
1 passing (5ms)
Now that our project is set up to properly run and execute tests, let's start writing some real tests for the application.