Skip to content

Commit

Permalink
fix(jest-runner): automatically set NODE_ENV env variable (#3816)
Browse files Browse the repository at this point in the history
Set `NODE_ENV` to `test` in the jest runner. This mimicks the way jest-cli internally works.
  • Loading branch information
wijtserekker committed Oct 29, 2022
1 parent f2cf7e6 commit 9fc7a6f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/jest-runner/src/jest-test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class JestTestRunner implements TestRunner {

try {
// Use process.env to set the active mutant.
// We could use `state.strykerStatic.activeMutant`, but that only works with the `StrykerEnvironment` mixin, wich is optional
// We could use `state.strykerStatic.activeMutant`, but that only works with the `StrykerEnvironment` mixin, which is optional
process.env[INSTRUMENTER_CONSTANTS.ACTIVE_MUTANT_ENV_VARIABLE] = activeMutant.id.toString();
const { dryRunResult } = await this.run({
fileNamesUnderTest: fileNameUnderTest ? [fileNameUnderTest] : undefined,
Expand Down Expand Up @@ -217,6 +217,8 @@ export class JestTestRunner implements TestRunner {
private setEnv() {
// Force colors off: https://github.com/chalk/supports-color#info
process.env.FORCE_COLOR = '0';
// Set node environment for issues like these: https://github.com/stryker-mutator/stryker-js/issues/3580
process.env.NODE_ENV = 'test';
}

private processTestResults(suiteResults: jestTestResult.TestResult[]): TestResult[] {
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-runner/test/unit/jest-test-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,16 @@ describe(JestTestRunner.name, () => {
expect(process.env[INSTRUMENTER_CONSTANTS.ACTIVE_MUTANT_ENV_VARIABLE]).to.equal(undefined);
});

it('should set the node environment variable before calling jest in the dry run', async () => {
const sut = createSut();
jestTestAdapterMock.run.callsFake(async () => {
expect(process.env.NODE_ENV).to.equal('test');
return jestRunResult;
});
await sut.dryRun(factory.dryRunOptions());
expect(jestTestAdapterMock.run.calledOnce).to.be.true;
});

it('should set the __strykerGlobalNamespace__ in globals', async () => {
const sut = createSut();
await sut.mutantRun(factory.mutantRunOptions({ activeMutant: factory.mutant({ id: '25' }) }));
Expand Down

0 comments on commit 9fc7a6f

Please sign in to comment.