Skip to content

Commit 6f8e918

Browse files
authored
fix: enforce import assertions when importing JSON in ESM (#12755)
1 parent e0b1249 commit 6f8e918

File tree

8 files changed

+218
-42
lines changed

8 files changed

+218
-42
lines changed

CHANGELOG.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414

1515
### Fixes
1616

17-
- `[jest-environment-node]` fix non-configurable globals ([#13687](https://github.com/facebook/jest/pull/13687))
17+
- `[jest-environment-node]` Fix non-configurable globals ([#13687](https://github.com/facebook/jest/pull/13687))
1818
- `[@jest/expect-utils]` `toMatchObject` should handle `Symbol` properties ([#13639](https://github.com/facebook/jest/pull/13639))
19-
- `[jest-mock]` fix mockReset and resetAllMocks undefined return ([#13692](https://github.com/facebook/jest/pull/13692))
19+
- `[jest-mock]` Fix `mockReset` and `resetAllMocks` `undefined` return value([#13692](https://github.com/facebook/jest/pull/13692))
2020
- `[jest-resolve]` Add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633))
21-
- `[jest-runtime]` Support Wasm files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608))
22-
- `[jest-runtime]` Using the scriptTransformer cache in jest-runner ([#13735](https://github.com/facebook/jest/pull/13735))
21+
- `[jest-runtime]` Support WASM files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608))
22+
- `[jest-runtime]` Use the `scriptTransformer` cache in `jest-runner` ([#13735](https://github.com/facebook/jest/pull/13735))
23+
- `[jest-runtime]` Enforce import assertions when importing JSON in ESM ([#12755](https://github.com/facebook/jest/pull/12755))
2324
- `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694))
2425
- `[jest-transform]` Ensure the correct configuration is passed to preprocessors specified multiple times in the `transform` option ([#13770](https://github.com/facebook/jest/pull/13770))
2526

e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`on node <16.12.0 does not enforce import assertions 1`] = `
4+
"Test Suites: 1 passed, 1 total
5+
Tests: 2 passed, 2 total
6+
Snapshots: 0 total
7+
Time: <<REPLACED>>
8+
Ran all test suites matching /native-esm-missing-import-assertions.test/i."
9+
`;
10+
311
exports[`on node >=16.9.0 support re-exports from CJS of dual packages 1`] = `
412
"Test Suites: 1 passed, 1 total
513
Tests: 1 passed, 1 total
@@ -8,6 +16,14 @@ Time: <<REPLACED>>
816
Ran all test suites matching /native-esm-deep-cjs-reexport.test.js/i."
917
`;
1018
19+
exports[`on node >=16.12.0 supports import assertions 1`] = `
20+
"Test Suites: 1 passed, 1 total
21+
Tests: 2 passed, 2 total
22+
Snapshots: 0 total
23+
Time: <<REPLACED>>
24+
Ran all test suites matching /native-esm-import-assertions.test/i."
25+
`;
26+
1127
exports[`runs WebAssembly (Wasm) test with native ESM 1`] = `
1228
"Test Suites: 1 passed, 1 total
1329
Tests: 6 passed, 6 total

e2e/__tests__/nativeEsm.test.ts

+63
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,66 @@ test('runs WebAssembly (Wasm) test with native ESM', () => {
7979
expect(stdout).toBe('');
8080
expect(exitCode).toBe(0);
8181
});
82+
83+
// version where `vm` API gets `import assertions`
84+
onNodeVersions('>=16.12.0', () => {
85+
test('enforces import assertions', () => {
86+
const {exitCode, stderr, stdout} = runJest(
87+
DIR,
88+
['native-esm-missing-import-assertions.test'],
89+
{nodeOptions: '--experimental-vm-modules --no-warnings'},
90+
);
91+
92+
const {rest} = extractSummary(stderr);
93+
94+
expect(rest).toContain(
95+
'package.json" needs an import assertion of type "json"',
96+
);
97+
expect(stdout).toBe('');
98+
expect(exitCode).toBe(1);
99+
});
100+
101+
test('supports import assertions', () => {
102+
const {exitCode, stderr, stdout} = runJest(
103+
DIR,
104+
['native-esm-import-assertions.test'],
105+
{nodeOptions: '--experimental-vm-modules --no-warnings'},
106+
);
107+
108+
const {summary} = extractSummary(stderr);
109+
110+
expect(summary).toMatchSnapshot();
111+
expect(stdout).toBe('');
112+
expect(exitCode).toBe(0);
113+
});
114+
});
115+
116+
onNodeVersions('<16.12.0', () => {
117+
test('does not enforce import assertions', () => {
118+
const {exitCode, stderr, stdout} = runJest(
119+
DIR,
120+
['native-esm-missing-import-assertions.test'],
121+
{nodeOptions: '--experimental-vm-modules --no-warnings'},
122+
);
123+
124+
const {summary} = extractSummary(stderr);
125+
126+
expect(summary).toMatchSnapshot();
127+
expect(stdout).toBe('');
128+
expect(exitCode).toBe(0);
129+
});
130+
131+
test('syntax error for import assertions', () => {
132+
const {exitCode, stderr, stdout} = runJest(
133+
DIR,
134+
['native-esm-import-assertions.test'],
135+
{nodeOptions: '--experimental-vm-modules --no-warnings'},
136+
);
137+
138+
const {rest} = extractSummary(stderr);
139+
140+
expect(rest).toContain('SyntaxError: Unexpected identifier');
141+
expect(stdout).toBe('');
142+
expect(exitCode).toBe(1);
143+
});
144+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import json from '../package.json' assert {type: 'json'};
9+
10+
test('supports static import', () => {
11+
expect(json).toHaveProperty('jest.testEnvironment', 'node');
12+
});
13+
14+
test('supports dynamic import', async () => {
15+
const {default: json} = await import('../package.json', {
16+
assert: {type: 'json'},
17+
});
18+
expect(json).toHaveProperty('jest.testEnvironment', 'node');
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import json from '../package.json';
9+
10+
test('supports static import', () => {
11+
expect(json).toHaveProperty('jest.testEnvironment', 'node');
12+
});
13+
14+
test('supports dynamic import', async () => {
15+
const {default: json} = await import('../package.json');
16+
expect(json).toHaveProperty('jest.testEnvironment', 'node');
17+
});

packages/jest-runtime/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@
3737
"jest-resolve": "workspace:^",
3838
"jest-snapshot": "workspace:^",
3939
"jest-util": "workspace:^",
40+
"semver": "^7.3.5",
4041
"slash": "^3.0.0",
4142
"strip-bom": "^4.0.0"
4243
},
4344
"devDependencies": {
4445
"@jest/test-utils": "workspace:^",
4546
"@types/glob": "^7.1.1",
4647
"@types/graceful-fs": "^4.1.3",
48+
"@types/semver": "^7.1.0",
4749
"jest-environment-node": "workspace:^"
4850
},
4951
"engines": {

0 commit comments

Comments
 (0)