Tiny, but fully loaded test-runner.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.6 KiB

'use strict';
const { arrayOrPush } = require('./util');
const caller = require('./lib/caller');
const emitter = require('./lib/emitter');
const reporters = require('./reporters');
const testrunner = require('./lib/testrunner');
const symbols = require('./util/symbols');
7 years ago
class Gunner {
constructor (name) {
this.name = name;
this.__suite__ = {
tests: [],
beforeHooks: {
[symbols.Start]: [],
[symbols.End]: [],
'*': [],
},
afterHooks: {
[symbols.Start]: [],
[symbols.End]: [],
'*': [],
}
};
7 years ago
}
test (description, test) {
const existing = (
this.__suite__.tests
.find(x => x.description === description)
);
if (existing)
throw new Error(`Test '${description}' already exists!`);
const unit = {
7 years ago
description,
type: 'test',
run: state => caller(test, state),
};
this.__suite__.tests.push(unit);
return this;
}
before (description, run, label) {
const unit = {
description,
label,
type: 'hook',
run: state => caller(run, state),
};
arrayOrPush(this.__suite__.beforeHooks, description, unit);
return this;
}
after (description, run, label) {
const unit = {
description,
label,
type: 'hook',
run: state => caller(run, state),
};
arrayOrPush(this.__suite__.afterHooks, description, unit);
return this;
7 years ago
}
run (options = {}) {
if (options.reporter === true)
reporters.default(emitter, options);
else if (typeof options.reporter === 'function')
options.reporter(emitter, options);
else if (reporters[options.reporter])
reporters[options.reporter](emitter, options);
emitter.emit('start');
return testrunner(this, options)
.then(results => {
results.count = results.length;
results.success = results.filter(r => r.status === 'ok');
results.failures = results.filter(r => r.status === 'notOk');
results.skipped = results.filter(r => r.status === 'skip');
results.successPercent = Math.floor(
results.success.length/results.length * 100
);
results.name = this.name;
if((results.successPercent !== 100)
&& typeof process !== 'undefined')
process.exitCode = 1;
emitter.emit('test end', results);
emitter.emit('end', results);
return (options.request
? {
[options.request]:
reporters[options.request].convert(results),
default: results }
: results);
});
7 years ago
}
}
const expect = require('./lib/expect');
7 years ago
module.exports = Gunner;
module.exports.expect = expect;
module.exports.expectMany = expect.expectMany;
module.exports.Start = symbols.Start;
module.exports.End = symbols.End;
module.exports.Gunner = module.exports;