sails-test-helper
Test helper suite for Sails.js using Mocha test framework.
Dependencies
Installation
# Local install $ npm install sails-test-helper # Global install $ sudo npm install -g sails-test-helper
Initialization
Copy test suite template to current directory.
# Local install $ node_modules/.bin/sails-test-helper init # Global install $ sails-test-helper init
The following will be copied to the current working directory:
Makefile
test/
factories/
fixtures/
helpers/
unit/
Writing Tests
Controllers
//-- test/unit/controllers/SampleController.test.js; ;
Execute SampleController test
$ make test
controllers/SampleController
GET index
✓ should be successful
1 passing
Models
//-- test/unit/models/Sample.test.js; ;
Execute Sample test
$ make test
models/Sample
.create()
✓ should be successful
1 passing
Test Execution
Tests are executed using make command. Basically the script will look for tests to be executed in test/unit/ directory.
# Run all tests $ make test # Run tests under a specific directory # This will run all tests under test/unit/controllers directory $ make test controllers # This will run tests under test/unit/controllers and test/unit/models directories $ make test controllers models # Run a specific test file # This will run tests in test/unit/controllers/SampleController.test.js file $ make test controllers/SampleController.test
Mocha Options
Mocha options can be passed as parameter to make. By default, mocha is being executed using the ff. options:
# recursive with 30 second timeout using spec reporter $ mocha --recursive -t 30000 -R spec
Use MOCHA_OPTS commandline variable to pass specific mocha options to make.
# Dot format without colors. Useful for test execution on CI servers such as Jenkins. $ make MOCHA_OPTS='-C -R dot' test
Helpers
- TEST_NAME
- TEST_ROOT_PATH
- TEST_HELPERS_PATH
- TEST_FACTORIES_PATH
- TEST_FIXTURES_PATH
- requireHelper()
- sinon
- stub()
- mock()
- chai
- expect()
- should
- request
- xhr
- factory
Custom Helpers
You can write your own test helpers or node modules and save it under test/helpers/ directory. Use the built-in requireHelper() function to load your custom helper.
//-- test/unit/services/SampleService.test.js; ;
If you need to do some initialization prior to all your tests execution, you can put them inside test/helpers/bootstrap.js file. This file will be loaded automatically upon test execution.
//-- test/helpers/bootstrap.js//-- global variables can also be initialized here... ;
Factories
You can use the built-in factory module to define, build or create factories.
//-- test/unit/controllers/SampleController.test.js; ;
You can also load your factory definitions from test/factories/ directory through your local bootstrap file.
//-- test/helpers/bootstrap.js;
Please see sails-factory for more details.