-
Notifications
You must be signed in to change notification settings - Fork 5
QA Tests and Continuous Integration
We have implemented a couple of very simple tests based on Mocha in order to start and test continuous integration.
Continuous integration for nodeJS with Travis-CI is documented.
Note: for our records, the equivalent of Travis-CI in the .NET world would be AppVeyor
First, sign up to Travis-CI with your GitHub account and enable the repository you need to track for continuous integration. If organization repositories are not listed, go to the organization members list and make your membership public.
Second, add a text file named .travis.yml at the root of the repository with the following content:
language: node_js
node_js:
- "0.11"
- "0.10"
services:
- mongodb
Third, make sure you have a test command by calling npm test
in a terminal session. This actually refers to the scripts object in package.json:
"scripts": {
"start": "node server.js",
"test": "mocha"
},
Finally, all npm package modules need to be installed locally. If you have installed mocha globally to benefit from the command line, you need to duplicate it locally in package.json so as to be installed by Travis-CI using npm install
:
"devDependencies": {
"mocha": "^1.18.2",
"supertest": "^0.12.1"
},
We need:
- Unit testing of nodeJS api endPoints with code coverage,
- Unit testing of Javascript client scripts on many browsers with code coverage,
- Functional (UI) testing (possibly in a headless browser).
The combination of Mocha and Chai fits these requirements on NodeJS servers and in browsers.
We also need to run these tests locally (with code coverage) and on Travis-CI (without code coverage). Considering there are multiple test tasks, we need to run them with Grunt and configure package.json so that the npm test
command actually launches grunt test
(see below).
First Mocha, Chai and SuperTest must be installed:
npm install -g mocha
npm install mocha --save-dev
npm install chai --save-dev
npm install supertest --save-dev
To install Grunt, open a nodeJS terminal prompt and execute the following commands:
npm install -g grunt-cli
npm install grunt --save-dev
Install with the following commands the Grunt tasks required to execute mocha test cases:
npm install grunt-env --save-dev
npm install grunt-mocha-test --save-dev
Then create a javascript file named Gruntfile.js at the project root, with the following content:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
env: {
test: { NODE_ENV: 'test' }
},
mochaTest: {
test: {
src: 'test/*.js',
options: {
ui: 'bdd',
reporter: 'spec'
}
}
}
});
// Load NpmTasks
grunt.loadNpmTasks('grunt-env');
grunt.loadNpmTasks('grunt-mocha-test');
// Default task(s).
grunt.registerTask('test', ['env:test', 'mochaTest:test']);
}
At this stage, check that you can successfully run grunt test
in a nodeJS terminal prompt and modify package.json so that npm test
actually executes grunt test
:
"scripts": {
"start": "node server.js",
"test": "grunt test"
},
Following this configuration, Travis-CI builds fail with error sh: 1: grunt: not found. The solution consists in modifying .travis.yml as follows:
language: node_js
node_js:
- "0.11"
- "0.10"
services:
- mongodb
before_script:
- npm install -g grunt-cli
Now our nodeJS unit tests are triggered by Grunt on Travis, allowing for more complexity.
??? TODO: Code Coverage.
Sources:
- http://shakyshane.com/beginners-guide-to-testing-nodejs-servers/
- http://www.jorisooms.be/testing-your-node-api-with-supertest/
- http://engineering.yp.com/post/js-testing-and-code-coverage
- http://thewayofcode.wordpress.com/2013/04/21/how-to-build-and-test-rest-api-with-nodejs-express-mocha/
- https://blog.mover.io/2012/10/31/api-testing-in-node-with-nock-mocha-and-supertest/
- http://www.gregjopa.com/2014/02/testing-and-code-coverage-with-node-js-apps/
- http://fredkschott.com/post/2014/05/nodejs-testing-essentials/
We can also use Mocha and Chai for testing Javascript client scripts in web browsers. The browser versions of Mocha and Chai can respectively be found here and here. Do not forget the mocha.css
stylesheet.
A mocha browser-based unit test consists of an html page and a javascript file.
grunt-mocha-test only runs nodeJS tests, not browser tests. To run browser tests with Mocha, install grunt-mocha from a nodeJS command prompt:
npm install grunt-mocha --save-dev
and configure it in Gruntfile.js as follow:
mocha: {
unit: {
src: ['test/unit/*.html'],
options: {
run: true
}
}
},
[...]
grunt.registerTask('test', ['mocha:unit']);
Then run grunt test
(or npm test
) to execute the tests in PhantomJS (a headless browser).
Karma will now let us execute the tests on multiple browsers with Instanbul code coverage.
To install Karma, run the following commands:
npm install -g karma-cli
npm install karma --save-dev
To configure Karma, run the following command and answer all questions to generate a configuration file:
karma init coverage.conf.js
This will also install npm dependencies including frameworks and browser adapters which you will find listed in package.json:
"devDependencies": {
[...]
"karma-mocha": "^0.1.3",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^0.1.3",
"karma-ie-launcher": "^0.1.5",
"karma-safari-launcher": "^0.1.1",
"karma-firefox-launcher": "^0.1.3",
"karma-phantomjs-launcher": "^0.1.4"
},
Karma requires that some files be configured in coverage.conf.js to be served by Karma server:
files: [
'src/js/vendor/jquery.min.js',
'src/js/vendor/kendo.all.min.js',
'src/js/*.js',
'test/unit/*.js',
],
Sources:
- https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/
- http://attackofzach.com/setting-up-a-project-using-karma-with-mocha-and-chai/
- http://robdodson.me/blog/2012/05/27/testing-backbone-boilerplate-with-mocha-and-chai/
Sources:
Source:
###Testing as a Service
- OpenSauce (from SauceLab) based on Selenium
- BrowserStack, also based on Selenium
- CodeSwarm (formerly BrowserSwarm)
- http://www.modern.ie/en-us/virtualization-tools#downloads
- https://ci.testling.com
- https://coveralls.io/
Copyright © 2013-2014 Memba Sarl. All rights reserved.