All files / shared / module-context.ts

100.00% Branches 4/4
100.00% Functions 4/4
100.00% Lines 25/25
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
 
 
x1
 
x1
x1
x1
 
x1
x114
x114
 
x1
x55
x55
 
 
x55
 
x55
x55
x55
x1
 
x1
x55
 
x55
 
x55
x55
x55
 
 
 
x55
 
x55
x55
x1






































import type Assert from './assert.ts';
import type { HookFn } from '../types.ts';
import TestContext from './test-context.ts';

export default class ModuleContext {
  static Assert: typeof Assert;
  static currentModuleChain: ModuleContext[] = [];

  static get lastModule() {
    return this.currentModuleChain[this.currentModuleChain.length - 1];
  }

  name!: string;
  assert!: Assert;
  userContext!: object;

  // Internal fallback assert for modules with no direct tests
  context = new TestContext();

  moduleChain: ModuleContext[] = [];
  beforeEachHooks: HookFn<Assert>[] = [];
  afterEachHooks: HookFn<Assert>[] = [];
  tests: TestContext[] = [];

  constructor(name: string) {
    const parentModule = ModuleContext.currentModuleChain[ModuleContext.currentModuleChain.length - 1];

    ModuleContext.currentModuleChain.push(this);

    this.moduleChain = ModuleContext.currentModuleChain.slice(0);
    this.name = parentModule ? `${parentModule.name} > ${name}` : name;
    this.assert = new ModuleContext.Assert(this);

    // User context uses prototype chain from parent module for proper QUnit-style inheritance:
    // parent before() sets props on parent userContext, child tests inherit via prototype.
    this.userContext = parentModule ? Object.create(parentModule.userContext) : Object.create(null);

    return Object.freeze(this);
  }
}