All files / shared / test-context.ts

94.44% Branches 17/18
100.00% Functions 6/6
98.46% Lines 64/65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
 
 
x3
x3
 
x3
x235
x235
x235
x235
x235
x235
x235
x235
 
x235
x235
x3
 
x3
x235
x160
x160
x160
x160
x160
x235
 
x3
x4
x1
x1
x1
 
x4
x1
x1
x4
x4
 
x3
x159
x2
x2
x2
x159
 
x3
x160
x159
x1
x1
x1
x1
x1
x1
x159
x1
x1
x1
x1
x1
x1
x158
x1
x1
x1
x1
x1
x1
x1
x160
x3

































I






































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}`,
      });
    }
  }
}