apigeek-affirm
Version:
A BDD/Gherkin micro-framework for REST APIs
95 lines (78 loc) • 3.3 kB
JavaScript
var Mocha = require("mocha");
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var assert = require('assert');
var cli = require("commander");
var WebHooks = require("./WebHooks");
// simple command line interface
// most options override the default test target
cli.usage("affirm [options]").
option("--config <config>", "A JSON config file").
option("--protocol <protocol>", "the default protocol (http)").
option("--hostname <hostname>", "the default hostname (localhost) for the test suite").
option("--port <port>", "the default port (80) for the test suite").
option("--basePath <basePath>", "the basePath is prefix of the resource").
option("--target <target>", "use a different target").
option("--files <path>", "folder containing mock files").
option("--features <path>", "folder containing test cases (.feature files)").
option("--tests <path>", "folder containing test cases (.feature files)")
cli.parse(process.argv);
// load / init configuration
var config = {};
try {
config = JSON.parse(fs.readFileSync( cli.config || 'affirm.json', 'utf8'));
} catch(e) {
console.log("warning: default configuration. Create an 'affirm.json' or --config <file>");
}
config.featuresPath = cli.tests || cli.features;
// target overrides defaults
if (cli.target) _.extend(config, config[cli.target]);
// ugly hack to inject some context into mocha test
global.affirm = _.extend(config, _.omit(cli, ["config", "tests", "features"]) );
// sensible mocha defaults
config.mocha = _.extend(config.mocha, { "timeout": 2000, "reporter": "spec", "ui": "bdd" } );
// start Mocha with Affirm Test Suite Runner
var mocha = new Mocha(config.mocha || {});
mocha.addFile( path.join(__dirname, "index.js") );
var webhooks = new WebHooks(global.affirm.webhooks);
var hasFailed = false;
var hasSuitFailed = true;
/**
* These are all the events you can subscribe to:
* - `start` execution started
* - `end` execution complete
* - `suite` (suite) test suite execution started
* - `suite end` (suite) all tests (and sub-suites) have finished
* - `test` (test) test execution started
* - `test end` (test) test completed
* - `hook` (hook) hook execution started
* - `hook end` (hook) hook complete
* - `pass` (test) test passed
* - `fail` (test, err) test failed
*/
mocha.run()
.on('suite', function(test) {
//console.log('Test Suite Start: %j -> %s %s', _.keys(test), test.type, test.title);
hasSuitFailed = false;
webhooks.started( { title: test.title });
})
.on('suite end', function(test) {
// console.log('Test done: '+test.title);
if (!hasSuitFailed) webhooks.success( { title: test.title });
else webhooks.failure( { title: test.title });
})
.on('pass', function(test) {
// console.log('Test Pass: %j -> %s %s', _.keys(test), test.type, test.title);
})
.on('fail', function(test, err) {
hasFailed = true;
hasSuitFailed = true;
})
.on('end', function(test) {
// after all suites has completed - emit failure or finished
test = test || {}
// console.log('Test End: %j -> %s %s', _.keys(test));
webhooks.finished( { title: "" });
});