fix(core): add tap.only option

This commit is contained in:
Philipp Kunz 2018-09-02 23:54:59 +02:00
parent 72b72a9ffb
commit 28b1f0bfed

View File

@ -4,28 +4,46 @@ import { TapTest, ITestFunction } from './tapbundle.classes.taptest';
import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap'; import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap';
export class Tap { export class Tap {
/** /**
* skip a test * skips a test
* tests marked with tap.skip.test() are never executed
*/ */
skip = { skip = {
test: (descriptionArg: string, functionArg: ITestFunction) => { test: (descriptionArg: string, functionArg: ITestFunction) => {
console.log(`skipped test: ${descriptionArg}`); console.log(`skipped test: ${descriptionArg}`);
},
testParallel: (descriptionArg: string, functionArg: ITestFunction) => {
console.log(`skipped test: ${descriptionArg}`);
} }
}; };
/**
* only executes tests marked as ONLY
*/
only = {
test: (descriptionArg: string, testFunctionArg: ITestFunction) => {
this.test(descriptionArg, testFunctionArg, 'only');
}
}
private _tapTests: TapTest[] = []; private _tapTests: TapTest[] = [];
private _tapTestsOnly: TapTest[] = [];
/** /**
* Normal test function, will run one by one * Normal test function, will run one by one
* @param testDescription - A description of what the test does * @param testDescription - A description of what the test does
* @param testFunction - A Function that returns a Promise and resolves or rejects * @param testFunction - A Function that returns a Promise and resolves or rejects
*/ */
async test(testDescription: string, testFunction: ITestFunction) { async test(testDescription: string, testFunction: ITestFunction, modeArg: 'normal' | 'only' | 'skip' = 'normal' ) {
let localTest = new TapTest({ let localTest = new TapTest({
description: testDescription, description: testDescription,
testFunction: testFunction, testFunction: testFunction,
parallel: false parallel: false
}); });
this._tapTests.push(localTest); if(modeArg === 'normal') {
this._tapTests.push(localTest);
} else if (modeArg === 'only') {
this._tapTestsOnly.push(localTest);
}
return localTest; return localTest;
} }
@ -63,9 +81,17 @@ export class Tap {
return; return;
} }
console.log(`1..${this._tapTests.length}`); // determine which tests to run
for (let testKey = 0; testKey < this._tapTests.length; testKey++) { let concerningTests: TapTest[];
let currentTest = this._tapTests[testKey]; if(this._tapTestsOnly.length > 0) {
concerningTests = this._tapTestsOnly;
} else {
concerningTests = this._tapTests;
}
console.log(`1..${concerningTests.length}`);
for (let testKey = 0; testKey < concerningTests.length; testKey++) {
let currentTest = concerningTests[testKey];
let testPromise = currentTest.run(testKey); let testPromise = currentTest.run(testKey);
if (currentTest.parallel) { if (currentTest.parallel) {
promiseArray.push(testPromise); promiseArray.push(testPromise);
@ -79,7 +105,7 @@ export class Tap {
let failReasons: string[] = []; let failReasons: string[] = [];
let executionNotes: string[] = []; let executionNotes: string[] = [];
// collect failed tests // collect failed tests
for (let tapTest of this._tapTests) { for (let tapTest of concerningTests) {
if (tapTest.status !== 'success') { if (tapTest.status !== 'success') {
failReasons.push( failReasons.push(
`Test ${tapTest.testKey + 1} failed with status ${tapTest.status}:\n` + `Test ${tapTest.testKey + 1} failed with status ${tapTest.status}:\n` +