Up to this point, all of the test code we've seen has just been demos and examples and we haven't actually run any tests. Let's set up the basic structure of our application so that we can start writing real tests.
The first thing to do is set up the folder structure that will house all of our tests. Within the root of the application project folder, create a folder named tests
. Within the tests
folder, create three more folders for controllers
, models
, and server
:
/(existing app root) tests/ ----/controllers/ ----/models/ ----/server/
Before we start writing the tests for our application, there's a small amount of overhead we need to take care of to prepare for our tests. To take care of this overhead, we're going to write a test helper 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:
var 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, but 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 globals
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:
$ 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 be run.
Executing that Mocha command from earlier at this point will simply return 0 passing (2ms)
because we don't have any tests yet. Let's write a quick sample test to make sure 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', function() { describe('First Test', function() { it('should assert 1 equals 1', function() { expect(1).to.equal(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 may find remembering and executing that long convoluted command for Mocha to be tiresome and frustrating. 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 providing this tweak to 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 other developers will know to simply execute npm test
whenever they want to run tests for your project.
Now that the package.json
file has been updated, you can simply execute npm test
to execute the suite of tests for the project:
$ 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 your project is set up to properly run and execute tests, let's start writing some real tests for the application.
18.118.163.159