Skip to content

Commit

Permalink
fix: properly handle re-exported native modules in ESM via CJS (#14589)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Oct 2, 2023
1 parent 11177a1 commit 9a9d14c
Show file tree
Hide file tree
Showing 8 changed files with 1,248 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- `[jest-config]` Make sure to respect `runInBand` option ([#14578](https://github.com/facebook/jest/pull/14578))
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
- `[jest-runtime]` Properly handle re-exported native modules in ESM via CJS ([#14589](https://github.com/jestjs/jest/pull/14589))
- `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552))
- `[pretty-format]` [**BREAKING**] Print `ArrayBuffer` and `DataView` correctly ([#14290](https://github.com/facebook/jest/pull/14290))

Expand Down
12 changes: 10 additions & 2 deletions e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`does not enforce import assertions 1`] = `
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /native-esm-missing-import-assertions.test/i."
Ran all test suites matching /native-esm-missing-import-assertions.test.js/i."
`;
exports[`on node >=16.11.0 support re-exports from CJS of dual packages 1`] = `
Expand All @@ -21,7 +21,15 @@ exports[`on node >=16.12.0 supports import assertions 1`] = `
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /native-esm-import-assertions.test/i."
Ran all test suites matching /native-esm-import-assertions.test.js/i."
`;
exports[`properly handle re-exported native modules in ESM via CJS 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /native-esm-native-module.test.js/i."
`;
exports[`runs WebAssembly (Wasm) test with native ESM 1`] = `
Expand Down
20 changes: 17 additions & 3 deletions e2e/__tests__/nativeEsm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,21 @@ test('runs WebAssembly (Wasm) test with native ESM', () => {
test('does not enforce import assertions', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-missing-import-assertions.test'],
['native-esm-missing-import-assertions.test.js'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

const {summary} = extractSummary(stderr);

expect(summary).toMatchSnapshot();
expect(stdout).toBe('');
expect(exitCode).toBe(0);
});

test('properly handle re-exported native modules in ESM via CJS', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-native-module.test.js'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

Expand All @@ -113,7 +127,7 @@ onNodeVersions('>=16.12.0', () => {
test('supports import assertions', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-import-assertions.test'],
['native-esm-import-assertions.test.js'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

Expand All @@ -129,7 +143,7 @@ onNodeVersions('<16.12.0', () => {
test('syntax error for import assertions', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-import-assertions.test'],
['native-esm-import-assertions.test.js'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

Expand Down
12 changes: 12 additions & 0 deletions e2e/native-esm/__tests__/native-esm-native-module.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import ivm from 'isolated-vm';

test('supports native modules exported via CJS', () => {
expect(ivm).toBeDefined();
});
1 change: 1 addition & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test('should support importing node core modules', () => {
devDependencies: {
'discord.js': '14.3.0',
'iso-constants': '^0.1.2',
'isolated-vm': '^4.6.0',
yargs: '^17.5.1',
},
jest: {
Expand Down
1 change: 1 addition & 0 deletions e2e/native-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"devDependencies": {
"discord.js": "14.3.0",
"iso-constants": "^0.1.2",
"isolated-vm": "^4.6.0",
"yargs": "^17.5.1"
},
"jest": {
Expand Down
Loading

0 comments on commit 9a9d14c

Please sign in to comment.