-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feat: experimental ES Modules support #9772
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
56d872b
feat: experimental ES Modules support
SimenB a9c1bfc
support importing node core modules
SimenB ad64ade
add test for dynamic import
SimenB d4b5d1d
Update e2e/__tests__/nativeEsm.test.ts
SimenB 988e791
renamed snap
SimenB 7bca6a2
clear registry on module reset
SimenB a40e22d
invariant over !
SimenB dc365a6
cache directory lookups as well
SimenB bb5c50a
remove import.meta.jest
SimenB 4e2630c
split out function
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`on node >=12.16.0 runs test with native ESM 1`] = ` | ||
Test Suites: 1 passed, 1 total | ||
Tests: 4 passed, 4 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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 {resolve} from 'path'; | ||
import wrap from 'jest-snapshot-serializer-raw'; | ||
import {onNodeVersions} from '@jest/test-utils'; | ||
import runJest, {getConfig} from '../runJest'; | ||
import {extractSummary} from '../Utils'; | ||
|
||
const DIR = resolve(__dirname, '../native-esm'); | ||
|
||
test('test config is without transform', () => { | ||
const {configs} = getConfig(DIR); | ||
|
||
expect(configs).toHaveLength(1); | ||
expect(configs[0].transform).toEqual([]); | ||
}); | ||
|
||
// The versions vm.Module was introduced | ||
onNodeVersions('>=12.16.0', () => { | ||
test('runs test with native ESM', () => { | ||
const {exitCode, stderr, stdout} = runJest(DIR, [], { | ||
nodeOptions: '--experimental-vm-modules', | ||
}); | ||
|
||
const {summary} = extractSummary(stderr); | ||
|
||
expect(wrap(summary)).toMatchSnapshot(); | ||
expect(stdout).toBe(''); | ||
expect(exitCode).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* 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 {readFileSync} from 'fs'; | ||
import {dirname, resolve} from 'path'; | ||
import {fileURLToPath} from 'url'; | ||
import {double} from '../index'; | ||
|
||
test('should have correct import.meta', () => { | ||
expect(typeof require).toBe('undefined'); | ||
expect(typeof jest).toBe('undefined'); | ||
expect(import.meta).toEqual({ | ||
url: expect.any(String), | ||
}); | ||
expect( | ||
import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js') | ||
).toBe(true); | ||
}); | ||
|
||
test('should double stuff', () => { | ||
expect(double(1)).toBe(2); | ||
}); | ||
|
||
test('should support importing node core modules', () => { | ||
const dir = dirname(fileURLToPath(import.meta.url)); | ||
const packageJsonPath = resolve(dir, '../package.json'); | ||
|
||
expect(JSON.parse(readFileSync(packageJsonPath, 'utf8'))).toEqual({ | ||
jest: { | ||
testEnvironment: 'node', | ||
transform: {}, | ||
}, | ||
type: 'module', | ||
}); | ||
}); | ||
|
||
test('dynamic import should work', async () => { | ||
const {double: importedDouble} = await import('../index'); | ||
|
||
expect(importedDouble).toBe(double); | ||
expect(importedDouble(1)).toBe(2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
export function double(num) { | ||
return num * 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "module", | ||
"jest": { | ||
"testEnvironment": "node", | ||
"transform": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* 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 {dirname, extname} from 'path'; | ||
// @ts-ignore: experimental, not added to the types | ||
import {SourceTextModule} from 'vm'; | ||
import type {Config} from '@jest/types'; | ||
import readPkgUp = require('read-pkg-up'); | ||
|
||
const runtimeSupportsVmModules = typeof SourceTextModule === 'function'; | ||
|
||
const cachedFileLookups = new Map<string, boolean>(); | ||
const cachedDirLookups = new Map<string, boolean>(); | ||
|
||
export function clearCachedLookups(): void { | ||
cachedFileLookups.clear(); | ||
cachedDirLookups.clear(); | ||
} | ||
|
||
export default function cachedShouldLoadAsEsm(path: Config.Path): boolean { | ||
let cachedLookup = cachedFileLookups.get(path); | ||
|
||
if (cachedLookup === undefined) { | ||
cachedLookup = shouldLoadAsEsm(path); | ||
cachedFileLookups.set(path, cachedLookup); | ||
} | ||
|
||
return cachedLookup; | ||
} | ||
|
||
// this is a bad version of what https://github.com/nodejs/modules/issues/393 would provide | ||
function shouldLoadAsEsm(path: Config.Path): boolean { | ||
if (!runtimeSupportsVmModules) { | ||
return false; | ||
} | ||
|
||
const extension = extname(path); | ||
|
||
if (extension === '.mjs') { | ||
return true; | ||
} | ||
|
||
if (extension === '.cjs') { | ||
return false; | ||
} | ||
|
||
// this isn't correct - we might wanna load any file as a module (using synthetic module) | ||
// do we need an option to Jest so people can opt in to ESM for non-js? | ||
if (extension !== '.js') { | ||
return false; | ||
} | ||
|
||
const cwd = dirname(path); | ||
|
||
let cachedLookup = cachedDirLookups.get(cwd); | ||
|
||
if (cachedLookup === undefined) { | ||
cachedLookup = cachedPkgCheck(cwd); | ||
cachedFileLookups.set(cwd, cachedLookup); | ||
} | ||
|
||
return cachedLookup; | ||
} | ||
|
||
function cachedPkgCheck(cwd: Config.Path): boolean { | ||
// TODO: can we cache lookups somehow? | ||
SimenB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const pkg = readPkgUp.sync({cwd, normalize: false}); | ||
|
||
if (!pkg) { | ||
return false; | ||
} | ||
|
||
return pkg.packageJson.type === 'module'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the issue with falling back to
pkg.packageJson.type === 'module';
for non-JS as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly because it's not how node would do it - it expects explicit loaders. This is wrong, but I think less wrong. This whole thing is gonna be implemented in
resolve
at some point regardless