Skip to content

Commit

Permalink
fix(jest): allow mixin jest env for unit testing (#3598)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicojs committed Jun 25, 2022
1 parent 4eaf713 commit da8a720
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function mixinJestEnvironment<T extends typeof JestEnvironment>(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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit da8a720

Please sign in to comment.