mapcomplete/testLegacy/TestHelper.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-03-23 19:48:06 +01:00
import {expect} from "chai"
export default class T {
public readonly name: string;
private readonly _tests: [string, (() => (void | Promise<void>))][];
2022-01-14 13:58:15 +01:00
constructor(tests: [string, () => (Promise<void> | void)][]) {
this.name = this.constructor.name;
this._tests = tests;
}
2021-11-07 16:34:51 +01:00
/**
* RUns the test, returns the error messages.
* Returns an empty list if successful
* @constructor
*/
2021-12-30 20:41:45 +01:00
public async Run(): Promise<{ testsuite: string, name: string, msg: string } []> {
2021-11-07 16:34:51 +01:00
const failures: { testsuite: string, name: string, msg: string } [] = []
for (const [name, test] of this._tests) {
try {
const r = test()
if (r instanceof Promise) {
2021-12-30 20:41:45 +01:00
try {
await r
} catch (e) {
console.log("ASYNC ERROR: ", e, e.stack)
failures.push({testsuite: this.name, name: name, msg: "" + e});
2021-12-30 20:41:45 +01:00
}
}
2021-11-07 16:34:51 +01:00
} catch (e) {
console.log("ERROR: ", e, e.stack)
failures.push({testsuite: this.name, name: name, msg: "" + e});
}
}
if (failures.length == 0) {
return undefined
} else {
return failures
}
}
}