Test driven development
Notes of working with nodejs, express, node-orm2 mocha, supertest and should.
Important points needed to know before starting tests designs:
Use a temporary Data store for all tests done.
Unit tests mean each test should be independent, thus essential data for the test should be created in it and not in other tests.
Identification of system’s main functionality is essential to organize your files and setups.
Test Organization
Mocha
Mocha allows you to organize your tests and reports. The docs found here can help as a guide with examples.
The main features:
Describe() — also called a suite, used to define a set of unit tests or set of “suites”
It() — define a unit test
Before(), After(), beforeEach(), afterEach() — hooks for running the tests, used for setup.
To use mocha, install it with “npm install –g mocha”. When running it, by default Mocha will find a test.js file under/test/ folder and run it. Other useful commands are:
“mocha -recursive” — Find all mocha files under “test/” folder and run them. ·
“mocha test/user/account-test.js” — Test only “test/user/account-test.js” file.
“mocha test/user/*.js“ —Test only js files immediately under “test/user/” directory.
Running the Test Application
Before and After hooks can be used for setup and cleanup respectively. The app can be executed in the Before hook as well as global variables such as the database object. Using configuration files here to define the database used can also be useful. By creating a global database object all data can be cleared on the After hook. Setting a longer time out with this.timeout(miliseconds) in these hooks might be needed so Mocha doesn’t freak out.
Prepare data for each section
Before and after hooks can also come handy when it is necessary to create data used on several suites. This hooks can be placed inside a describe() and will be executed once before and after all the unit tests inside it are executed.
Create data for each unit test
Data is sometimes needed to test features that often modify it. As in unit testing tests should not depend on the outcomes of other tests, its data needs to be fresh. BeforeEach and AfterEach are handy in this situation.
Manual data reset
In case that something that Mocha isn’t expecting goes wrong the After hooks might not be triggered so a manual reset of the whole test database will be neded.
Creating unit tests
Each unit test is defined with it() function that expects 2 parameter: a description of what it should do and a function to be executed.
Supertest
Supertest is the library that allows http requests to an API and check response headers. Also, it allows creating requests with authentication needed. If used with Mocha, exceptions thrown will be caught and reported. Requests made with supertest library usually goes inside an it() function of Mocha.
Should
Should is a simple library that allows checking results of a request in more detail than Supertest. These results could be checked either directly from the response, or searching through the database for results generated by the made.