Unit Testing - pcimino/nodejs-restify-mongodb GitHub Wiki
Unit testing
Anti Pattern
In the process of learning Mocha and figuring out how to use the framework with restify and superagent. The real ugliness is where I'm using the script definitions in the package.json. I should build a makefile and use that, but that creates other challenges I'm not willing to face at the moment.
Simple
This assumes the package.json setup works on your Windows environment. Details below, in case you need to modify for your OS/machine.
To run unit tests:
- You've already setup this project to run (See Quick Setup )
- MongoDB is running
Keep in mind that if you change the application configuration, you need to kill the node process before rerunning the tests. the test script kicks off node, but it keeps running in the background.
Manual Testing
- Start the Mongo Database :
start-mongo.sh
(.bat) - Start the node application :
start-node.sh
(.bat) - Re-initialize the database test data :
npm run-script ./scripts/unittest-database-setup
- Run the tests:
npm test
- Optionally clean up the test database (the setup purges/resets the data) :
npm run-script ./scripts/unittest-database-teardown
- Stop Mongo :
stop-mongo.sh
(.bat)
NPM Scripts
In the package.json, there are script commands defined which can be executed via npm:
npm run-script [script name]
Apologies to Mac/*nix. Currently these are defined to use the Windows batch scripts. You can look in the .BAT files and see how to run these from the shell on other platforms.
Database Setup
To set up the database, run the unittest-database-setup.bat or script command:
npm run-script unittest-database-setup
Unit Test
To run the unit tests:
npm run-script test
Database Teardown
To clean up the database, run the unittest-database-teardown.bat or script command:
npm run-script unittest-database-teardown
Return Home