Skip to content

Commit

Permalink
fix(js): babel preset should also check for JEST_WORKER_ID to transpi…
Browse files Browse the repository at this point in the history
…le to CJS (#21754)

(cherry picked from commit 70ea82d)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Feb 9, 2024
1 parent 4adc24f commit 73a013c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions e2e/jest/src/jest.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import {
cleanupProject,
expectJestTestsToPass,
newProject,
runCLI,
runCLIAsync,
uniq,
updateFile,
expectJestTestsToPass,
cleanupProject,
} from '@nx/e2e/utils';

describe('Jest', () => {
Expand All @@ -20,6 +20,21 @@ describe('Jest', () => {
await expectJestTestsToPass('@nx/js:lib --unitTestRunner=jest');
}, 500000);

it('should be resilient against NODE_ENV values', async () => {
const name = uniq('lib');
runCLI(
`generate @nx/js:lib ${name} --unitTestRunner=jest --no-interactive`
);

const results = await runCLIAsync(`test ${name}`, {
env: {
...process.env, // need to set this for some reason, or else get "env: node: No such file or directory"
NODE_ENV: 'foobar',
},
});
expect(results.combinedOutput).toContain('Test Suites: 1 passed, 1 total');
});

it('should merge with jest config globals', async () => {
const testGlobal = `'My Test Global'`;
const mylib = uniq('mylib');
Expand Down
2 changes: 1 addition & 1 deletion packages/js/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function (api: any, options: NxWebBabelPresetOptions = {}) {
// For Jest tests, NODE_ENV is set as 'test' and we only want to set target as Node.
// All other options will fail in Jest since Node does not support some ES features
// such as import syntax.
isTest || process.env.NODE_ENV === 'test'
isTest || process.env.NODE_ENV === 'test' || process.env.JEST_WORKER_ID
? { targets: { node: 'current' }, loose: true }
: {
// Allow importing core-js in entrypoint and use browserslist to select polyfills.
Expand Down

0 comments on commit 73a013c

Please sign in to comment.