Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): babel preset should also check for JEST_WORKER_ID to transpile to CJS #21754

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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