How to test a sailsjs application
We have built an app that logs a list of jokes for each user.
Create a test (unit and acceptence) to make sure that
- if a user request a joke
- That same joke is added to the user's joke list
-
Look at the sailsjs-handlebars-app tutorial to create a handlebars based sails app.
-
Look at sails-record-list tutorial to finish up the app logic
-
save user info that includes email
-
add/fetch joke for the user
-
Add a
test
folder
mkdir test
- Add the rest a structure
<app>/api
|- test # mocha default testing folder
| |- mocha.opts
| |- unit # testing a function/method as it is (same input give out same output)
| | |- controllers
| | |- models
| | |- policies
| | |- services
| | | |- DailyJokeService.test.js
| | | |- UtilityService.test.js
| |- integration # testing the behavior of a function when placed in a given environment (ex: action handler defined in the controller)
| | |- controllers
| | | |- UserController.test.js
| | |- models
| | |- policies
| | |- services
| | | |- DatabaseAccessService.test.js
- install required packages
npm install supertest --save-dev
npm install should --save-dev
-
create a test - user can be created using /user/create api
-
add mocha configuration values inside
mocha.opts
# /test/mocha.opts
--timeout 20s
test/bootstrap.test.js
- Add a testing script inside
package.json
"test": "istanbul cover node_modules/.bin/_mocha -- -- test/integration/controllers/**/*.test.js"
- run the test
npm install -g mocha istanbul
npm test