All files / shared / module-context.ts

100.00% Branches 4/4
100.00% Functions 4/4
100.00% Lines 27/27
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
 
 
x3
 
x3
x3
x3
 
x3
x152
x152
x152
 
x3
x69
x69
 
 
x69
 
x69
x69
x69
x3
 
x3
x69
x69
 
x69
 
x69
x69
x69
 
 
 
x69
 
x69
x69
x3








































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

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

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

  name!: string;
  assert!: Assert;
  userContext!: Record<string, unknown>;

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

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

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

    chain.push(this);

    this.moduleChain = chain.slice();
    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);
  }
}