tapbundle/ts/tapbundle.classes.taptest.ts

79 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as plugins from './tapbundle.plugins';
import { tapCreator } from './tapbundle.tapcreator';
import { TapTools } from './tapbundle.classes.taptools';
2017-04-28 07:49:57 +00:00
// imported interfaces
2018-08-08 21:38:43 +00:00
import { Deferred } from '@pushrocks/smartpromise';
2020-07-07 22:54:15 +00:00
import { HrtMeasurement } from '@pushrocks/smarttime';
2017-04-28 07:49:57 +00:00
// interfaces
export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout';
2017-04-28 07:49:57 +00:00
export type ITestFunction = (tapTools?: TapTools) => Promise<any>;
2017-04-28 07:49:57 +00:00
export class TapTest {
public description: string;
public failureAllowed: boolean;
public hrtMeasurement: HrtMeasurement;
public parallel: boolean;
public status: TTestStatus;
public tapTools: TapTools;
public testFunction: ITestFunction;
public testKey: number; // the testKey the position in the test qeue. Set upon calling .run()
public testDeferred: Deferred<TapTest> = plugins.smartpromise.defer();
public testPromise: Promise<TapTest> = this.testDeferred.promise;
2017-04-28 07:49:57 +00:00
/**
* 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;
2017-04-28 07:49:57 +00:00
}
/**
* run the test
*/
public async run(testKeyArg: number) {
this.hrtMeasurement.start();
this.testKey = testKeyArg;
const testNumber = testKeyArg + 1;
2017-04-28 07:49:57 +00:00
try {
2020-07-07 22:54:15 +00:00
await this.testFunction(this.tapTools);
2017-04-28 07:49:57 +00:00
if (this.status === 'timeout') {
throw new Error('Test succeeded, but timed out...');
2017-04-28 07:49:57 +00:00
}
this.hrtMeasurement.stop();
console.log(
`ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`
);
this.status = 'success';
this.testDeferred.resolve(this);
2017-04-28 07:49:57 +00:00
} catch (err) {
this.hrtMeasurement.stop();
console.log(
`not ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`
);
this.testDeferred.resolve(this);
2017-05-04 21:15:08 +00:00
// if the test has already succeeded before
2017-04-28 07:49:57 +00:00
if (this.status === 'success') {
this.status = 'errorAfterSuccess';
console.log('!!! ALERT !!!: weird behaviour, since test has been already successfull');
2017-07-11 13:10:05 +00:00
} else {
this.status = 'error';
2017-04-28 07:49:57 +00:00
}
2017-05-04 21:15:08 +00:00
// if the test is allowed to fail
if (this.failureAllowed) {
console.log(`please note: failure allowed!`);
2017-04-28 07:49:57 +00:00
}
console.log(err);
2017-04-28 07:49:57 +00:00
}
}
}