apigeek-affirm
Version:
A BDD/Gherkin micro-framework for REST APIs
129 lines (92 loc) • 4.36 kB
JavaScript
var Yadda = require('yadda'); // https://github.com/acuminous/yadda
var assert = require("assert");
var fs = require("fs");
var _ = require("lodash");
var http = require("./helpers/http")
//var WebHooks = require("./WebHooks")
module.exports = function(config) {
config = config || {};
_.defaults(config, {
yadda: {
l16n: "English",
featuresPath: config.featuresPath || "features",
stepsPath: "./steps/"
},
files: config.files || "files",
basePath: ""
})
if (config.debug) console.log("TestSuite: %j", config);
assert(config.yadda.featuresPath, "Missing 'featuresPath'");
assert(config.yadda.stepsPath, "Missing 'stepsPath'");
assert(config.yadda.l16n, "Missing 'Language'");
config.target = config.target || {};
//var webhooks = new WebHooks(config.webhooks);
Yadda.plugins.mocha.StepLevelPlugin.init();
// initialize the local language files
var Locale = Yadda.localisation[config.yadda.l16n];
var dictionary = new Yadda.Dictionary()
dictionary.define('CSV', /([^\u0000]*)/, require("./converter/csv") );
dictionary.define('JSON', /([^\u0000]*)/, require("./converter/json") );
//dictionary.define('xml', /([^\u0000]*)/, require("./converter/xml") );
//dictionary.define('text', /([^\u0000]*)/, require("./converter/text") );
var library = new Locale.library(dictionary);
// load the built-in steps (gherkin vocabulary support)
var stepsFiles = fs.readdirSync(__dirname+"/"+config.yadda.stepsPath);
_.each(stepsFiles, function(file) {
require(config.yadda.stepsPath+file)(library, config);
})
// initialize feature scoped variables
var feature_scope = { feature: _.pick(config,["hostname", "protocol", "port", "basePath" ])};
_.defaults(feature_scope, { "protocol": "http", "port": 80, basePath: "" });
feature_scope.feature.vars = {};
feature_scope.cookies = http.cookies();
// setup the Yadda framework (parse and test runner)
var yadda = this.yadda = new Yadda.Yadda(library, feature_scope );
var events = this.events = Yadda.EventBus.instance();
// execute the features
new Yadda.FeatureFileSearch(config.yadda.featuresPath).each(function (file) {
featureFile(file, function (feature) {
feature_scope.target = config.target[feature.annotations.target] || feature_scope.target || {};
if (feature.annotations.verbose) {
events.on(Yadda.EventBus.ON_EXECUTE, function(event) {
console.log("%s -> %j", event.name, event.data);
});
}
if (feature.annotations.requires) {
var required = feature.annotations.requires.split(",");
_.each(required, function(pkg) {
pkg && require(pkg);
})
}
//events.on(Yadda.EventBus.ON_EXECUTE, function(event) {
// webhooks.started(event);
//});
//events.on(Yadda.EventBus.ON_SCENARIO, function(event) {
// console.log("ON_SCENARIO: %s -> %j", event.name, event.data);
// webhooks.started(event);
//});
scenarios(feature.scenarios, function(scenario) {
var scenario_scope = {
request: { headers: {}, qs: {} },
response: {},
stopwatch: {},
vars: {}
};
scenario_scope.target = _.extend({},feature_scope, feature_scope.target, config.target[scenario.annotations.target]);
if (scenario.annotations.verbose) {
events.on(Yadda.EventBus.ON_SCENARIO, function(event) {
console.log("%s -> %j", event.name, event.data);
});
}
var is_async = (scenario.annotations.async)?true:!scenario.annotations.sync;
var doStepAsync = function(step, done) {
yadda.run(step, scenario_scope, done);
}
var doStepSync = function(step) {
yadda.run(step, scenario_scope);
}
steps(scenario.steps, is_async?doStepAsync:doStepSync);
});
});
});
};