2022-03-14 10:22:17 +00:00
|
|
|
import * as plugins from './tapbundle.plugins.js';
|
2017-04-23 09:10:13 +00:00
|
|
|
|
2022-03-14 10:22:17 +00:00
|
|
|
import { IPreTaskFunction, PreTask } from './tapbundle.classes.pretask.js';
|
|
|
|
import { TapTest, ITestFunction } from './tapbundle.classes.taptest.js';
|
2021-01-26 03:15:50 +00:00
|
|
|
export class Tap <T> {
|
2017-07-05 22:06:18 +00:00
|
|
|
/**
|
2018-09-02 21:54:59 +00:00
|
|
|
* skips a test
|
|
|
|
* tests marked with tap.skip.test() are never executed
|
2017-07-05 22:06:18 +00:00
|
|
|
*/
|
2019-11-16 18:22:20 +00:00
|
|
|
public skip = {
|
2021-01-26 03:19:49 +00:00
|
|
|
test: (descriptionArg: string, functionArg: ITestFunction<T>) => {
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log(`skipped test: ${descriptionArg}`);
|
2018-09-02 21:54:59 +00:00
|
|
|
},
|
2021-01-26 03:19:49 +00:00
|
|
|
testParallel: (descriptionArg: string, functionArg: ITestFunction<T>) => {
|
2018-09-02 21:54:59 +00:00
|
|
|
console.log(`skipped test: ${descriptionArg}`);
|
2020-07-08 00:17:25 +00:00
|
|
|
},
|
2018-06-28 21:34:41 +00:00
|
|
|
};
|
2017-04-28 09:00:22 +00:00
|
|
|
|
2018-09-02 21:54:59 +00:00
|
|
|
/**
|
|
|
|
* only executes tests marked as ONLY
|
|
|
|
*/
|
2019-11-16 18:22:20 +00:00
|
|
|
public only = {
|
2021-01-26 03:15:50 +00:00
|
|
|
test: (descriptionArg: string, testFunctionArg: ITestFunction<T>) => {
|
2018-09-02 21:54:59 +00:00
|
|
|
this.test(descriptionArg, testFunctionArg, 'only');
|
2020-07-08 00:17:25 +00:00
|
|
|
},
|
2019-04-10 10:56:17 +00:00
|
|
|
};
|
2018-09-02 21:54:59 +00:00
|
|
|
|
2019-11-16 22:40:36 +00:00
|
|
|
private _tapPreTasks: PreTask[] = [];
|
2021-01-26 03:15:50 +00:00
|
|
|
private _tapTests: TapTest<any>[] = [];
|
|
|
|
private _tapTestsOnly: TapTest<any>[] = [];
|
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
|
|
|
|
*/
|
2021-01-26 02:35:02 +00:00
|
|
|
public test(
|
2019-04-10 10:56:17 +00:00
|
|
|
testDescription: string,
|
2021-01-26 03:15:50 +00:00
|
|
|
testFunction: ITestFunction<T>,
|
2019-04-10 10:56:17 +00:00
|
|
|
modeArg: 'normal' | 'only' | 'skip' = 'normal'
|
2021-01-26 03:19:49 +00:00
|
|
|
): TapTest<T> {
|
2021-01-26 03:15:50 +00:00
|
|
|
const localTest = new TapTest<T>({
|
2017-04-23 09:10:13 +00:00
|
|
|
description: testDescription,
|
2019-11-16 18:22:20 +00:00
|
|
|
testFunction,
|
2020-07-08 00:17:25 +00:00
|
|
|
parallel: false,
|
2018-06-28 21:34:41 +00:00
|
|
|
});
|
2019-04-10 10:56:17 +00:00
|
|
|
if (modeArg === 'normal') {
|
2018-09-02 21:54:59 +00:00
|
|
|
this._tapTests.push(localTest);
|
|
|
|
} else if (modeArg === 'only') {
|
|
|
|
this._tapTestsOnly.push(localTest);
|
|
|
|
}
|
2018-06-28 21:34:41 +00:00
|
|
|
return localTest;
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 00:17:25 +00:00
|
|
|
public preTask(descriptionArg: string, functionArg: IPreTaskFunction) {
|
2020-03-15 16:36:25 +00:00
|
|
|
this._tapPreTasks.push(new PreTask(descriptionArg, functionArg));
|
2019-11-16 22:40:36 +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
|
|
|
|
*/
|
2021-01-26 03:19:49 +00:00
|
|
|
public testParallel(testDescription: string, testFunction: ITestFunction<T>) {
|
2018-06-28 21:34:41 +00:00
|
|
|
this._tapTests.push(
|
|
|
|
new TapTest({
|
|
|
|
description: testDescription,
|
2019-11-16 18:22:20 +00:00
|
|
|
testFunction,
|
2020-07-08 00:17:25 +00:00
|
|
|
parallel: true,
|
2018-06-28 21:34:41 +00:00
|
|
|
})
|
|
|
|
);
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* starts the test evaluation
|
|
|
|
*/
|
2019-11-16 18:22:20 +00:00
|
|
|
public async start(optionsArg?: { throwOnError: boolean }) {
|
2020-07-08 00:11:16 +00:00
|
|
|
// lets set the tapbundle promise
|
|
|
|
const smartenvInstance = new plugins.smartenv.Smartenv();
|
2020-07-08 00:17:25 +00:00
|
|
|
smartenvInstance.isBrowser
|
2021-12-10 16:34:06 +00:00
|
|
|
? ((globalThis as any).tapbundleDeferred = plugins.smartpromise.defer())
|
2020-07-08 00:17:25 +00:00
|
|
|
: null;
|
2020-07-08 00:11:16 +00:00
|
|
|
|
|
|
|
// lets continue with running the tests
|
2019-11-16 18:22:20 +00:00
|
|
|
const promiseArray: Array<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!');
|
2019-11-16 22:40:36 +00:00
|
|
|
// TODO: throw proper error
|
2018-06-28 21:34:41 +00:00
|
|
|
return;
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 21:54:59 +00:00
|
|
|
// determine which tests to run
|
|
|
|
let concerningTests: TapTest[];
|
2019-04-10 10:56:17 +00:00
|
|
|
if (this._tapTestsOnly.length > 0) {
|
2018-09-02 21:54:59 +00:00
|
|
|
concerningTests = this._tapTestsOnly;
|
|
|
|
} else {
|
|
|
|
concerningTests = this._tapTests;
|
|
|
|
}
|
|
|
|
|
2019-11-16 22:40:36 +00:00
|
|
|
// lets run the pretasks
|
|
|
|
for (const preTask of this._tapPreTasks) {
|
|
|
|
await preTask.run();
|
|
|
|
}
|
|
|
|
|
2018-09-02 21:54:59 +00:00
|
|
|
console.log(`1..${concerningTests.length}`);
|
|
|
|
for (let testKey = 0; testKey < concerningTests.length; testKey++) {
|
2019-11-16 18:22:20 +00:00
|
|
|
const currentTest = concerningTests[testKey];
|
|
|
|
const 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
|
2019-11-16 18:22:20 +00:00
|
|
|
const failReasons: string[] = [];
|
|
|
|
const executionNotes: string[] = [];
|
2017-07-11 13:10:05 +00:00
|
|
|
// collect failed tests
|
2019-11-16 18:22:20 +00:00
|
|
|
for (const tapTest of concerningTests) {
|
2017-07-11 13:10:05 +00:00
|
|
|
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
|
2019-11-16 18:22:20 +00:00
|
|
|
for (const 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) {
|
2020-07-08 00:46:43 +00:00
|
|
|
if (!smartenvInstance.isBrowser) process.exit(1);
|
|
|
|
}
|
|
|
|
if (smartenvInstance.isBrowser) {
|
2021-12-10 16:34:06 +00:00
|
|
|
(globalThis as any).tapbundleDeferred.resolve();
|
2017-07-11 13:10:05 +00:00
|
|
|
}
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handle errors
|
|
|
|
*/
|
2021-12-10 16:34:06 +00:00
|
|
|
public threw(err: Error) {
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log(err);
|
2017-04-23 09:10:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 21:34:41 +00:00
|
|
|
export let tap = new Tap();
|