All files / shared / test-context.ts

100.00% Branches 9/9
80.00% Functions 16/20
86.96% Lines 80/92
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
 
 
x1
x1
 
x1
x169
x2
x2
x169
x114
x114
 
x169
x169
x514
x514
x169
x114
x114
 
x169
x169
x235
x235
 
 
 
 
x169
x169
x489
x489
x169
x114
x114
 
x169
 
 
 
 
 
 
 
x169
x169
x178
x178
 
 
 
 
x169
x169
x137
x137
x169
x25
x25
 
x169
x169
x833
x833
x169
x693
x693
 
x1
 
x1
x169
x114
x114
x114
x114
x114
x169
 
x1
x114
x1
x1
x1
x1
x1
x1
x114
x1
x1
x1
x1
x1
x1
x113
x1
x1
x1
x1
x1
x1
x1
x114
x1
 









































































































import type Assert from './assert.ts';
import type ModuleContext from './module-context.ts';

export default class TestContext {
  static Assert: typeof Assert;

  #name: string | undefined;
  get name() {
    return this.#name;
  }
  set name(value) {
    this.#name = value;
  }

  #module: ModuleContext | undefined;
  get module() {
    return this.#module;
  }
  set module(value) {
    this.#module = value;
  }

  #asyncOps: Promise<void>[] = [];
  get asyncOps() {
    return this.#asyncOps;
  }
  set asyncOps(value) {
    this.#asyncOps = value;
  }

  #assert: Assert | undefined;
  get assert() {
    return this.#assert;
  }
  set assert(value) {
    this.#assert = value;
  }

  #timeout: number | undefined;
  get timeout() {
    return this.#timeout;
  }
  set timeout(value) {
    this.#timeout = value;
  }

  #steps: string[] = [];
  get steps() {
    return this.#steps;
  }
  set steps(value) {
    this.#steps = value;
  }

  #expectedAssertionCount: number | undefined;
  get expectedAssertionCount() {
    return this.#expectedAssertionCount;
  }
  set expectedAssertionCount(value) {
    this.#expectedAssertionCount = value;
  }

  #totalExecutedAssertions = 0;
  get totalExecutedAssertions() {
    return this.#totalExecutedAssertions;
  }
  set totalExecutedAssertions(value) {
    this.#totalExecutedAssertions = value;
  }

  userContext: object = {};

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

  finish() {
    if (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}`,
      });
    } else 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 && 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}`,
      });
    }
  }
}