Skip to content

Commit

Permalink
fix: enforce import assertions when importing JSON in ESM (#12755)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 24, 2023
1 parent e0b1249 commit 6f8e918
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 42 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Expand Up @@ -14,12 +14,13 @@

### Fixes

- `[jest-environment-node]` fix non-configurable globals ([#13687](https://github.com/facebook/jest/pull/13687))
- `[jest-environment-node]` Fix non-configurable globals ([#13687](https://github.com/facebook/jest/pull/13687))
- `[@jest/expect-utils]` `toMatchObject` should handle `Symbol` properties ([#13639](https://github.com/facebook/jest/pull/13639))
- `[jest-mock]` fix mockReset and resetAllMocks undefined return ([#13692](https://github.com/facebook/jest/pull/13692))
- `[jest-mock]` Fix `mockReset` and `resetAllMocks` `undefined` return value([#13692](https://github.com/facebook/jest/pull/13692))
- `[jest-resolve]` Add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633))
- `[jest-runtime]` Support Wasm files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608))
- `[jest-runtime]` Using the scriptTransformer cache in jest-runner ([#13735](https://github.com/facebook/jest/pull/13735))
- `[jest-runtime]` Support WASM files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608))
- `[jest-runtime]` Use the `scriptTransformer` cache in `jest-runner` ([#13735](https://github.com/facebook/jest/pull/13735))
- `[jest-runtime]` Enforce import assertions when importing JSON in ESM ([#12755](https://github.com/facebook/jest/pull/12755))
- `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694))
- `[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))

Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
@@ -1,5 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`on node <16.12.0 does not enforce import assertions 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /native-esm-missing-import-assertions.test/i."
`;
exports[`on node >=16.9.0 support re-exports from CJS of dual packages 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Expand All @@ -8,6 +16,14 @@ Time: <<REPLACED>>
Ran all test suites matching /native-esm-deep-cjs-reexport.test.js/i."
`;
exports[`on node >=16.12.0 supports import assertions 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /native-esm-import-assertions.test/i."
`;
exports[`runs WebAssembly (Wasm) test with native ESM 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 6 passed, 6 total
Expand Down
63 changes: 63 additions & 0 deletions e2e/__tests__/nativeEsm.test.ts
Expand Up @@ -79,3 +79,66 @@ test('runs WebAssembly (Wasm) test with native ESM', () => {
expect(stdout).toBe('');
expect(exitCode).toBe(0);
});

// version where `vm` API gets `import assertions`
onNodeVersions('>=16.12.0', () => {
test('enforces import assertions', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-missing-import-assertions.test'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

const {rest} = extractSummary(stderr);

expect(rest).toContain(
'package.json" needs an import assertion of type "json"',
);
expect(stdout).toBe('');
expect(exitCode).toBe(1);
});

test('supports import assertions', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-import-assertions.test'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

const {summary} = extractSummary(stderr);

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

onNodeVersions('<16.12.0', () => {
test('does not enforce import assertions', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-missing-import-assertions.test'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

const {summary} = extractSummary(stderr);

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

test('syntax error for import assertions', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-import-assertions.test'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

const {rest} = extractSummary(stderr);

expect(rest).toContain('SyntaxError: Unexpected identifier');
expect(stdout).toBe('');
expect(exitCode).toBe(1);
});
});
19 changes: 19 additions & 0 deletions e2e/native-esm/__tests__/native-esm-import-assertions.test.js
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import json from '../package.json' assert {type: 'json'};

test('supports static import', () => {
expect(json).toHaveProperty('jest.testEnvironment', 'node');
});

test('supports dynamic import', async () => {
const {default: json} = await import('../package.json', {
assert: {type: 'json'},
});
expect(json).toHaveProperty('jest.testEnvironment', 'node');
});
@@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import json from '../package.json';

test('supports static import', () => {
expect(json).toHaveProperty('jest.testEnvironment', 'node');
});

test('supports dynamic import', async () => {
const {default: json} = await import('../package.json');
expect(json).toHaveProperty('jest.testEnvironment', 'node');
});
2 changes: 2 additions & 0 deletions packages/jest-runtime/package.json
Expand Up @@ -37,13 +37,15 @@
"jest-resolve": "workspace:^",
"jest-snapshot": "workspace:^",
"jest-util": "workspace:^",
"semver": "^7.3.5",
"slash": "^3.0.0",
"strip-bom": "^4.0.0"
},
"devDependencies": {
"@jest/test-utils": "workspace:^",
"@types/glob": "^7.1.1",
"@types/graceful-fs": "^4.1.3",
"@types/semver": "^7.1.0",
"jest-environment-node": "workspace:^"
},
"engines": {
Expand Down

0 comments on commit 6f8e918

Please sign in to comment.