-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental ES Modules support (#9772)
- Loading branch information
Showing
18 changed files
with
457 additions
and
31 deletions.
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? | ||
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.