node.js |
|
---|---|
Using Jasmine with nodeThe Jasmine node package contains helper code for developing and running Jasmine tests for node-based projects. |
|
Install |
|
You can install Jasmine using npm, locally in your project and globally to use the CLI tool. |
npm install jasmine
npm install -g jasmine
|
Init a Project |
|
Initialize a project for Jasmine by creating a spec directory and configuration json for you. |
jasmine init
|
Generate examples |
|
Generate example spec and source files |
jasmine examples
|
Configuration |
|
Customize
|
{
|
Spec directory path. Your spec_files must be relative to this path |
"spec_dir": "spec",
|
Array of filepaths (and globs) relative to spec_dir to include |
"spec_files": [
"**/*[sS]pec.js"
],
|
Array of filepaths (and globs) relative spec_dir to include before jasmine specs |
"helpers": [
"helpers/**/*.js"
],
|
Stop execution of a spec after the first expectation failure in it |
"stopSpecOnExpectationFailure": false,
|
Run specs in semi-random order |
"random": false
}
|
Running tests |
|
Once you have set up your Pass a relative path to a spec file to the jasmine command to only execute specs in a single file. |
jasmine
jasmine spec/appSpec.js
|
CLI Options |
|
|
JASMINE_CONFIG_PATH=spec/config/jasmine.json jasmine
jasmine JASMINE_CONFIG_PATH=spec/config/jasmine.json
|
|
jasmine --no-color
|
|
jasmine --filter="a spec name"
|
|
jasmine --stop-on-failure=true
|
|
jasmine --random=true
|
|
jasmine --seed=4321
|
Using the library |
|
If you want more granular control over the configuration, Jasmine can also be used as a library in your project. This allows you to load multiple config files or control your configuration in different ways. |
var Jasmine = require('jasmine');
var jasmine = new Jasmine();
|
Load configuration from a file or from an object. |
jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.loadConfig({
spec_dir: 'spec',
spec_files: [
'appSpec.js',
'requests/**/*[sS]pec.js',
'utils/**/*[sS]pec.js'
],
helpers: [
'helpers/**/*.js'
]
});
|
Custom onComplete |
|
Optionally specify a custom |
jasmine.onComplete(function(passed) {
if(passed) {
console.log('All specs have passed');
}
else {
console.log('At least one spec has failed');
}
});
|
Reporters |
|
A ConsoleReporter is included if no other reporters are added.
You can configure the default reporter with |
jasmine.configureDefaultReporter({
|
The |
timer: new jasmine.jasmine.Timer(),
|
The |
print: function() {
process.stdout.write(arguments);
},
|
|
showColors: true
});
|
You can add a custom reporter with |
var CustomReporter = require('./myCustomReporter');
var customReporter = new CustomReporter();
jasmine.addReporter(customReporter);
|
Run the tests |
|
Calling |
jasmine.execute();
|
|
jasmine.execute(['fooSpec.js'], 'a spec name');
|
A simple example using the library |
var Jasmine = require('jasmine');
var jasmine = new Jasmine();
jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.configureDefaultReporter({
showColors: false
});
jasmine.execute();
|