From da8a7206243f148030bf7421d236fd5b3be87b89 Mon Sep 17 00:00:00 2001 From: Nico Jansen Date: Sat, 25 Jun 2022 22:50:42 +0200 Subject: [PATCH] fix(jest): allow mixin jest env for unit testing (#3598) Allow `mixinJestEnvironment` to be used under normal unit testing without Stryker. This way was already documented, but broke in v6 release: https://stryker-mutator.io/docs/stryker-js/jest-runner/#coverage-reporting --- .../jest-plugins/cjs/mixin-jest-environment.ts | 2 +- .../jest-plugins/mixin-jest-environment.spec.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/jest-runner/src/jest-plugins/cjs/mixin-jest-environment.ts b/packages/jest-runner/src/jest-plugins/cjs/mixin-jest-environment.ts index 709c4c78e9..f69f9020b2 100644 --- a/packages/jest-runner/src/jest-plugins/cjs/mixin-jest-environment.ts +++ b/packages/jest-runner/src/jest-plugins/cjs/mixin-jest-environment.ts @@ -36,7 +36,7 @@ export function mixinJestEnvironment(JestEnvir constructor(config: JestEnvironmentConfig, context: EnvironmentContext) { super(config, context); - this.strykerContext = this.global[this.global.__strykerGlobalNamespace__] = state.instrumenterContext; + this.strykerContext = this.global[this.global.__strykerGlobalNamespace__ ?? '__stryker__'] = state.instrumenterContext; state.testFilesWithStrykerEnvironment.add(context.testPath); } diff --git a/packages/jest-runner/test/unit/jest-plugins/mixin-jest-environment.spec.ts b/packages/jest-runner/test/unit/jest-plugins/mixin-jest-environment.spec.ts index 5ce30b681c..db8898a1b9 100644 --- a/packages/jest-runner/test/unit/jest-plugins/mixin-jest-environment.spec.ts +++ b/packages/jest-runner/test/unit/jest-plugins/mixin-jest-environment.spec.ts @@ -38,6 +38,23 @@ describe(`jest plugins ${mixinJestEnvironment.name}`, () => { expect(sut.global.__stryker2__).eq(state.instrumenterContext); }); + it('should default to __stryker__ when there is no global stryker variable name configured', () => { + // Arrange + state.clear(); + + // Act + const sut = new (mixinJestEnvironment( + class extends JestEnvironmentNode { + public async handleTestEvent(_event: Circus.Event, _eventState: Circus.State) { + // Idle + } + } + ))(producers.createEnvironmentConfig(), producers.createEnvironmentContext()); + + // Assert + expect(sut.global.__stryker__).eq(state.instrumenterContext); + }); + it('should add the testPath to the test files with stryker environment', async () => { // Arrange state.clear();