From 9fc7a6f64b27cdb67e6844ce00f6e55c630d0cd6 Mon Sep 17 00:00:00 2001 From: Wijtse Rekker Date: Sat, 29 Oct 2022 10:14:12 +0200 Subject: [PATCH] fix(jest-runner): automatically set `NODE_ENV` env variable (#3816) Set `NODE_ENV` to `test` in the jest runner. This mimicks the way jest-cli internally works. --- packages/jest-runner/src/jest-test-runner.ts | 4 +++- .../jest-runner/test/unit/jest-test-runner.spec.ts | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/jest-runner/src/jest-test-runner.ts b/packages/jest-runner/src/jest-test-runner.ts index e37f986836..46e631ef17 100644 --- a/packages/jest-runner/src/jest-test-runner.ts +++ b/packages/jest-runner/src/jest-test-runner.ts @@ -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, @@ -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[] { diff --git a/packages/jest-runner/test/unit/jest-test-runner.spec.ts b/packages/jest-runner/test/unit/jest-test-runner.spec.ts index c54c8aad7d..4da50c153f 100644 --- a/packages/jest-runner/test/unit/jest-test-runner.spec.ts +++ b/packages/jest-runner/test/unit/jest-test-runner.spec.ts @@ -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' }) }));