import type Assert from './assert.ts';
import type ModuleContext from './module-context.ts';
export default class TestContext {
static Assert: typeof Assert;
name: string | undefined;
module: ModuleContext | undefined;
asyncOps: Promise<void>[] = [];
assert: Assert | undefined;
timeout: number | undefined;
steps: string[] = [];
expectedAssertionCount: number | undefined;
totalExecutedAssertions = 0;
userContext: Record<string, unknown> = {};
rejectTimeout: ((err: Error) => void) | undefined;
#timeoutHandle: ReturnType<typeof setTimeout> | undefined;
#timedOut = false;
constructor(name?: string, moduleContext?: ModuleContext) {
if (moduleContext) {
this.name = `${moduleContext.name} | ${name}`;
this.module = moduleContext;
this.module.tests.push(this);
this.assert = new TestContext.Assert(moduleContext, this);
}
}
setTimeoutDuration(ms: number) {
if (this.#timeoutHandle !== undefined) {
clearTimeout(this.#timeoutHandle);
this.#timeoutHandle = undefined;
}
if (!this.rejectTimeout) return;
this.#timeoutHandle = setTimeout(() => {
this.#timedOut = true;
this.rejectTimeout!(new Error(`Test timed out after ${ms}ms`));
}, ms);
}
clearTimeoutHandle() {
if (this.#timeoutHandle !== undefined) {
clearTimeout(this.#timeoutHandle);
this.#timeoutHandle = undefined;
}
}
finish() {
if (this.#timedOut) return;
if (this.steps.length > 0) {
this.assert!.pushResult({
result: false,
actual: this.steps,
expected: [],
message: `Expected assert.verifySteps() to be called before end of test after using assert.step(). Unverified steps: ${this.steps.join(', ')}`,
});
} else if (this.expectedAssertionCount !== undefined && this.expectedAssertionCount !== this.totalExecutedAssertions) {
this.assert!.pushResult({
result: false,
actual: this.totalExecutedAssertions,
expected: this.expectedAssertionCount,
message: `Expected ${this.expectedAssertionCount} assertions, but ${this.totalExecutedAssertions} were run for test: ${this.name}`,
});
} else if (this.expectedAssertionCount === undefined && this.totalExecutedAssertions === 0) {
this.assert!.pushResult({
result: false,
actual: this.totalExecutedAssertions,
expected: '> 0',
message: `Expected at least one assertion to be run for test: ${this.name}`,
});
}
}
}
|