2018-06-28 21:34:41 +00:00
|
|
|
import * as plugins from './tapbundle.plugins';
|
2017-04-23 09:10:13 +00:00
|
|
|
|
2018-06-28 21:34:41 +00:00
|
|
|
import { TapTest, ITestFunction } from './tapbundle.classes.taptest';
|
|
|
|
import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap';
|
2017-04-23 09:10:13 +00:00
|
|
|
export class Tap {
|
2017-07-05 22:06:18 +00:00
|
|
|
/**
|
|
|
|
* skip a test
|
|
|
|
*/
|
2017-04-28 09:00:22 +00:00
|
|
|
skip = {
|
2017-07-05 22:06:18 +00:00
|
|
|
test: (descriptionArg: string, functionArg: ITestFunction) => {
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log(`skipped test: ${descriptionArg}`);
|
2017-04-28 09:00:22 +00:00
|
|
|
}
|
2018-06-28 21:34:41 +00:00
|
|
|
};
|
2017-04-28 09:00:22 +00:00
|
|
|
|
2018-06-28 21:34:41 +00:00
|
|
|
private _tapTests: TapTest[] = [];
|
2017-04-23 09:10:13 +00:00
|
|
|
|
2017-04-23 12:35:16 +00:00
|
|
|
/**
|
|
|
|
* Normal test function, will run one by one
|
|
|
|
* @param testDescription - A description of what the test does
|
|
|
|
* @param testFunction - A Function that returns a Promise and resolves or rejects
|
|
|
|
*/
|
2018-06-28 21:34:41 +00:00
|
|
|
async test(testDescription: string, testFunction: ITestFunction) {
|
2017-04-28 07:49:57 +00:00
|
|
|
let localTest = new TapTest({
|
2017-04-23 09:10:13 +00:00
|
|
|
description: testDescription,
|
|
|
|
testFunction: testFunction,
|
|
|
|
parallel: false
|
2018-06-28 21:34:41 +00:00
|
|
|
});
|
|
|
|
this._tapTests.push(localTest);
|
|
|
|
return localTest;
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 22:06:18 +00:00
|
|
|
/**
|
|
|
|
* wraps function
|
|
|
|
*/
|
2018-06-28 21:34:41 +00:00
|
|
|
wrap(functionArg: ITapWrapFunction) {
|
|
|
|
return new TapWrap(functionArg);
|
2017-07-05 22:06:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 12:35:16 +00:00
|
|
|
/**
|
|
|
|
* A parallel test that will not be waited for before the next starts.
|
|
|
|
* @param testDescription - A description of what the test does
|
|
|
|
* @param testFunction - A Function that returns a Promise and resolves or rejects
|
|
|
|
*/
|
2018-06-28 21:34:41 +00:00
|
|
|
testParallel(testDescription: string, testFunction: ITestFunction) {
|
|
|
|
this._tapTests.push(
|
|
|
|
new TapTest({
|
|
|
|
description: testDescription,
|
|
|
|
testFunction: testFunction,
|
|
|
|
parallel: true
|
|
|
|
})
|
|
|
|
);
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* starts the test evaluation
|
|
|
|
*/
|
2018-06-28 21:34:41 +00:00
|
|
|
async start(optionsArg?: { throwOnError: boolean }) {
|
|
|
|
let promiseArray: Promise<any>[] = [];
|
2017-04-23 09:10:13 +00:00
|
|
|
|
|
|
|
// safeguard against empty test array
|
2017-07-11 13:10:05 +00:00
|
|
|
if (this._tapTests.length === 0) {
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log('no tests specified. Ending here!');
|
|
|
|
return;
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log(`1..${this._tapTests.length}`);
|
2017-07-11 13:10:05 +00:00
|
|
|
for (let testKey = 0; testKey < this._tapTests.length; testKey++) {
|
2018-06-28 21:34:41 +00:00
|
|
|
let currentTest = this._tapTests[testKey];
|
|
|
|
let testPromise = currentTest.run(testKey);
|
2017-04-23 09:10:13 +00:00
|
|
|
if (currentTest.parallel) {
|
2018-06-28 21:34:41 +00:00
|
|
|
promiseArray.push(testPromise);
|
2017-04-23 09:10:13 +00:00
|
|
|
} else {
|
2018-06-28 21:34:41 +00:00
|
|
|
await testPromise;
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-28 21:34:41 +00:00
|
|
|
await Promise.all(promiseArray);
|
2017-07-11 13:10:05 +00:00
|
|
|
|
|
|
|
// when tests have been run and all promises are fullfilled
|
2018-06-28 21:34:41 +00:00
|
|
|
let failReasons: string[] = [];
|
|
|
|
let executionNotes: string[] = [];
|
2017-07-11 13:10:05 +00:00
|
|
|
// collect failed tests
|
|
|
|
for (let tapTest of this._tapTests) {
|
|
|
|
if (tapTest.status !== 'success') {
|
|
|
|
failReasons.push(
|
2018-06-28 21:34:41 +00:00
|
|
|
`Test ${tapTest.testKey + 1} failed with status ${tapTest.status}:\n` +
|
|
|
|
`|| ${tapTest.description}\n` +
|
|
|
|
`|| for more information please take a look the logs above`
|
|
|
|
);
|
2017-07-11 13:10:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// render fail Reasons
|
|
|
|
for (let failReason of failReasons) {
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log(failReason);
|
2017-07-11 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optionsArg && optionsArg.throwOnError && failReasons.length > 0) {
|
2018-06-28 21:34:41 +00:00
|
|
|
process.exit(1);
|
2017-07-11 13:10:05 +00:00
|
|
|
}
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handle errors
|
|
|
|
*/
|
2018-06-28 21:34:41 +00:00
|
|
|
threw(err) {
|
|
|
|
console.log(err);
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 21:34:41 +00:00
|
|
|
export let tap = new Tap();
|