1 | import type { JestEnvironment, EnvironmentContext, JestEnvironmentConfig } from '@jest/environment';
|
2 | import type { Circus } from '@jest/types';
|
3 | import { InstrumenterContext } from '@stryker-mutator/api/core';
|
4 |
|
5 | import { state } from './messaging.js';
|
6 |
|
7 | function fullNameDescribeBlock(describe: Circus.DescribeBlock): string {
|
8 | if (describe.parent) {
|
9 | const parentName = fullNameDescribeBlock(describe.parent);
|
10 | return `${parentName} ${describe.name}`.trimStart();
|
11 | } else {
|
12 | return '';
|
13 | }
|
14 | }
|
15 |
|
16 | function fullName(test: Circus.TestEntry): string {
|
17 | const suiteName = fullNameDescribeBlock(test.parent);
|
18 | return `${suiteName} ${test.name}`.trimStart();
|
19 | }
|
20 |
|
21 | const STRYKER_JEST_ENV = Symbol('StrykerJestEnvironment');
|
22 |
|
23 | export function mixinJestEnvironment<T extends typeof JestEnvironment>(JestEnvironmentClass: T & { [STRYKER_JEST_ENV]?: true }): T {
|
24 | if (JestEnvironmentClass[STRYKER_JEST_ENV]) {
|
25 | return JestEnvironmentClass;
|
26 | } else {
|
27 | class StrykerJestEnvironment extends JestEnvironmentClass {
|
28 |
|
29 |
|
30 | |
31 |
|
32 |
|
33 | private readonly strykerContext: InstrumenterContext;
|
34 |
|
35 | public static readonly [STRYKER_JEST_ENV] = true;
|
36 |
|
37 | constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
|
38 | super(config, context);
|
39 | this.strykerContext = this.global[this.global.__strykerGlobalNamespace__] = state.instrumenterContext;
|
40 | state.testFilesWithStrykerEnvironment.add(context.testPath);
|
41 | }
|
42 |
|
43 | public handleTestEvent: Circus.EventHandler = async (event: Circus.Event, eventState: Circus.State) => {
|
44 |
|
45 | await super.handleTestEvent?.(event as any, eventState);
|
46 | if (state.coverageAnalysis === 'perTest') {
|
47 | if (event.name === 'test_start') {
|
48 | this.strykerContext.currentTestId = fullName(event.test);
|
49 | } else if (event.name === 'test_done') {
|
50 | this.strykerContext.currentTestId = undefined;
|
51 | }
|
52 | }
|
53 | };
|
54 | }
|
55 | return StrykerJestEnvironment;
|
56 | }
|
57 | }
|