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