fix(core): fix node 10.x.x compatibility

This commit is contained in:
Phil Kunz
2018-06-28 23:34:41 +02:00
parent 0028c5492b
commit 3199943b59
31 changed files with 681 additions and 1096 deletions

View File

@ -1,2 +1,2 @@
export { expect } from 'smartchai'
export { tap } from './tapbundle.classes.tap'
export { expect } from 'smartchai';
export { tap } from './tapbundle.classes.tap';

View File

@ -1,40 +1,39 @@
import * as plugins from './tapbundle.plugins'
import * as plugins from './tapbundle.plugins';
import { TapTest, ITestFunction } from './tapbundle.classes.taptest'
import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap'
import { TapTest, ITestFunction } from './tapbundle.classes.taptest';
import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap';
export class Tap {
/**
* skip a test
*/
skip = {
test: (descriptionArg: string, functionArg: ITestFunction) => {
console.log(`skipped test: ${descriptionArg}`)
console.log(`skipped test: ${descriptionArg}`);
}
}
};
private _tapTests: TapTest[] = []
private _tapTests: TapTest[] = [];
/**
* 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
*/
async test (testDescription: string, testFunction: ITestFunction) {
async test(testDescription: string, testFunction: ITestFunction) {
let localTest = new TapTest({
description: testDescription,
testFunction: testFunction,
parallel: false
})
this._tapTests.push(localTest)
return localTest
});
this._tapTests.push(localTest);
return localTest;
}
/**
* wraps function
*/
wrap (functionArg: ITapWrapFunction) {
return new TapWrap(functionArg)
wrap(functionArg: ITapWrapFunction) {
return new TapWrap(functionArg);
}
/**
@ -42,71 +41,70 @@ export class Tap {
* @param testDescription - A description of what the test does
* @param testFunction - A Function that returns a Promise and resolves or rejects
*/
testParallel (testDescription: string, testFunction: ITestFunction) {
this._tapTests.push(new TapTest({
description: testDescription,
testFunction: testFunction,
parallel: true
}))
testParallel(testDescription: string, testFunction: ITestFunction) {
this._tapTests.push(
new TapTest({
description: testDescription,
testFunction: testFunction,
parallel: true
})
);
}
/**
* starts the test evaluation
*/
async start(optionsArg?: {
throwOnError: boolean
}) {
let promiseArray: Promise<any>[] = []
async start(optionsArg?: { throwOnError: boolean }) {
let promiseArray: Promise<any>[] = [];
// safeguard against empty test array
if (this._tapTests.length === 0) {
console.log('no tests specified. Ending here!')
return
console.log('no tests specified. Ending here!');
return;
}
console.log(`1..${this._tapTests.length}`)
console.log(`1..${this._tapTests.length}`);
for (let testKey = 0; testKey < this._tapTests.length; testKey++) {
let currentTest = this._tapTests[testKey]
let testPromise = currentTest.run(testKey)
let currentTest = this._tapTests[testKey];
let testPromise = currentTest.run(testKey);
if (currentTest.parallel) {
promiseArray.push(testPromise)
promiseArray.push(testPromise);
} else {
await testPromise
await testPromise;
}
}
await Promise.all(promiseArray)
await Promise.all(promiseArray);
// when tests have been run and all promises are fullfilled
let failReasons: string[] = []
let executionNotes: string[] = []
let failReasons: string[] = [];
let executionNotes: string[] = [];
// collect failed tests
for (let tapTest of this._tapTests) {
if (tapTest.status !== 'success') {
failReasons.push(
`Test ${tapTest.testKey + 1} failed with status ${tapTest.status}:\n`
+ `|| ${tapTest.description}\n`
+ `|| for more information please take a look the logs above`
)
`Test ${tapTest.testKey + 1} failed with status ${tapTest.status}:\n` +
`|| ${tapTest.description}\n` +
`|| for more information please take a look the logs above`
);
}
}
// render fail Reasons
for (let failReason of failReasons) {
console.log(failReason)
console.log(failReason);
}
if (optionsArg && optionsArg.throwOnError && failReasons.length > 0) {
process.exit(1)
process.exit(1);
}
}
/**
* handle errors
*/
threw (err) {
console.log(err)
threw(err) {
console.log(err);
}
}
export let tap = new Tap()
export let tap = new Tap();

View File

@ -1,80 +1,79 @@
import * as plugins from './tapbundle.plugins'
import { tapCreator } from './tapbundle.tapcreator'
import { TapTools } from './tapbundle.classes.taptools'
import * as plugins from './tapbundle.plugins';
import { tapCreator } from './tapbundle.tapcreator';
import { TapTools } from './tapbundle.classes.taptools';
// imported interfaces
import { HrtMeasurement } from 'early'
import { Deferred } from 'smartq'
import { HrtMeasurement } from 'early';
import { Deferred } from 'smartq';
// interfaces
export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout'
export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout';
export interface ITestFunction {
(tapTools?: TapTools): Promise<any>
(tapTools?: TapTools): Promise<any>;
}
export class TapTest {
description: string
failureAllowed: boolean
hrtMeasurement: HrtMeasurement
parallel: boolean
status: TTestStatus
tapTools: TapTools
testFunction: ITestFunction
testKey: number // the testKey the position in the test qeue. Set upon calling .run()
testDeferred: Deferred<TapTest> = plugins.smartq.defer()
testPromise: Promise<TapTest> = this.testDeferred.promise
description: string;
failureAllowed: boolean;
hrtMeasurement: HrtMeasurement;
parallel: boolean;
status: TTestStatus;
tapTools: TapTools;
testFunction: ITestFunction;
testKey: number; // the testKey the position in the test qeue. Set upon calling .run()
testDeferred: Deferred<TapTest> = plugins.smartq.defer();
testPromise: Promise<TapTest> = this.testDeferred.promise;
/**
* constructor
*/
constructor (optionsArg: {
description: string,
testFunction: ITestFunction,
parallel: boolean
}) {
this.description = optionsArg.description
this.hrtMeasurement = new HrtMeasurement()
this.parallel = optionsArg.parallel
this.status = 'pending'
this.tapTools = new TapTools(this)
this.testFunction = optionsArg.testFunction
constructor(optionsArg: { description: string; testFunction: ITestFunction; parallel: boolean }) {
this.description = optionsArg.description;
this.hrtMeasurement = new HrtMeasurement();
this.parallel = optionsArg.parallel;
this.status = 'pending';
this.tapTools = new TapTools(this);
this.testFunction = optionsArg.testFunction;
}
/**
* run the test
*/
async run (testKeyArg: number) {
this.hrtMeasurement.start()
this.testKey = testKeyArg
let testNumber = testKeyArg + 1
async run(testKeyArg: number) {
this.hrtMeasurement.start();
this.testKey = testKeyArg;
let testNumber = testKeyArg + 1;
try {
await this.testFunction(this.tapTools)
await this.testFunction(this.tapTools);
if (this.status === 'timeout') {
throw new Error ('Test succeeded, but timed out...')
throw new Error('Test succeeded, but timed out...');
}
this.hrtMeasurement.stop()
console.log(`ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`)
this.status = 'success'
this.testDeferred.resolve(this)
this.hrtMeasurement.stop();
console.log(
`ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`
);
this.status = 'success';
this.testDeferred.resolve(this);
} catch (err) {
this.hrtMeasurement.stop()
console.log(`not ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`)
this.testDeferred.resolve(this)
this.hrtMeasurement.stop();
console.log(
`not ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`
);
this.testDeferred.resolve(this);
// if the test has already succeeded before
if (this.status === 'success') {
this.status = 'errorAfterSuccess'
console.log('!!! ALERT !!!: weird behaviour, since test has been already successfull')
this.status = 'errorAfterSuccess';
console.log('!!! ALERT !!!: weird behaviour, since test has been already successfull');
} else {
this.status = 'error'
this.status = 'error';
}
// if the test is allowed to fail
if (this.failureAllowed) {
console.log(`please note: failure allowed!`)
console.log(`please note: failure allowed!`);
}
console.log(err)
console.log(err);
}
}
}

View File

@ -1,59 +1,58 @@
import * as plugins from './tapbundle.plugins'
import { TapTest } from './tapbundle.classes.taptest'
import * as plugins from './tapbundle.plugins';
import { TapTest } from './tapbundle.classes.taptest';
export interface IPromiseFunc {
(): Promise<any>
(): Promise<any>;
}
export class TapTools {
/**
* the referenced TapTest
*/
private _tapTest: TapTest
private _tapTest: TapTest;
constructor (TapTestArg) {
this._tapTest = TapTestArg
constructor(TapTestArg) {
this._tapTest = TapTestArg;
}
/**
* allow failure
*/
allowFailure () {
this._tapTest.failureAllowed = true
allowFailure() {
this._tapTest.failureAllowed = true;
}
/**
* async/await delay method
*/
async delayFor (timeMilliArg) {
await plugins.smartdelay.delayFor(timeMilliArg)
async delayFor(timeMilliArg) {
await plugins.smartdelay.delayFor(timeMilliArg);
}
async delayForRandom (timeMilliMinArg, timeMilliMaxArg) {
await plugins.smartdelay.delayForRandom(timeMilliMinArg, timeMilliMaxArg)
async delayForRandom(timeMilliMinArg, timeMilliMaxArg) {
await plugins.smartdelay.delayForRandom(timeMilliMinArg, timeMilliMaxArg);
}
async timeout (timeMilliArg: number) {
let timeout = new plugins.smartdelay.Timeout(timeMilliArg)
timeout.makeUnrefed()
await timeout.promise
async timeout(timeMilliArg: number) {
let timeout = new plugins.smartdelay.Timeout(timeMilliArg);
timeout.makeUnrefed();
await timeout.promise;
if (this._tapTest.status === 'pending') {
this._tapTest.status = 'timeout'
this._tapTest.status = 'timeout';
}
}
async checkIterationLeak (iterationfuncArg: IPromiseFunc) {
await plugins.leakage.iterate.async(iterationfuncArg)
async checkIterationLeak(iterationfuncArg: IPromiseFunc) {
await plugins.leakage.iterate.async(iterationfuncArg);
}
async returnError (throwingFuncArg: IPromiseFunc) {
let funcErr: Error
async returnError(throwingFuncArg: IPromiseFunc) {
let funcErr: Error;
try {
await throwingFuncArg()
await throwingFuncArg();
} catch (err) {
funcErr = err
funcErr = err;
}
return funcErr
return funcErr;
}
}

View File

@ -1,24 +1,24 @@
import * as plugins from './tapbundle.plugins'
import * as plugins from './tapbundle.plugins';
export interface ITapWrapFunction {
(): Promise<any>
(): Promise<any>;
}
export class TapWrap {
wrapFunction: ITapWrapFunction
wrapFunction: ITapWrapFunction;
/**
* the constructor
*/
constructor (wrapFunctionArg: ITapWrapFunction) {
constructor(wrapFunctionArg: ITapWrapFunction) {
// nothing here
this.wrapFunction = wrapFunctionArg
this.wrapFunction = wrapFunctionArg;
}
/**
* run the wrapFunction
*/
async run () {
await this.wrapFunction()
async run() {
await this.wrapFunction();
}
}

View File

@ -1,11 +1,6 @@
import * as early from 'early'
import * as leakage from 'leakage'
import * as smartdelay from 'smartdelay'
import * as smartq from 'smartq'
import * as early from 'early';
import * as leakage from 'leakage';
import * as smartdelay from 'smartdelay';
import * as smartq from 'smartq';
export {
early,
smartdelay,
smartq,
leakage
}
export { early, smartdelay, smartq, leakage };

View File

@ -1,7 +1,7 @@
import * as plugins from './tapbundle.plugins'
import * as plugins from './tapbundle.plugins';
export class TapCreator {
// TODO:
}
export let tapCreator = new TapCreator()
export let tapCreator = new TapCreator();