-
-
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
fix(jest-config): add mjs
and cjs
to default moduleFileExtensions
config
#12578
Conversation
4b93f32
to
e010106
Compare
We should add |
mjs
to default moduleFileExtensions
configmjs
and cjs
to default moduleFileExtensions
config
@SimenB done. |
@@ -759,7 +759,9 @@ class ScriptTransformer { | |||
} | |||
}, | |||
{ | |||
exts: this._config.moduleFileExtensions.map(ext => `.${ext}`), | |||
exts: this._config.moduleFileExtensions | |||
.filter(ext => ext !== 'mjs') |
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.
why this?
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.
transform.test.ts is fail when including mjs in the extension list.
$ ./packages/jest/bin/jest.js '/Users/feng/dev/jest/e2e/__tests__/transform.test.ts'
transform-esm-runner
✕ runs test with native ESM (618 ms)
transform-esm-testrunner
✕ runs test with native ESM (868 ms)
● on node >=12.17.0 › transform-esm-runner › runs test with native ESM
Can't parse JSON.
ERROR: SyntaxError Unexpected end of JSON input
STDOUT:
STDERR: /Users/feng/dev/jest/e2e/transform/transform-esm-runner/runner.mjs:8
import testResult from '@jest/test-result';
^^^^^^
SyntaxError: Cannot use import statement outside a module
146 | };
147 | } catch (e: any) {
> 148 | throw new Error(
| ^
149 | `
150 | Can't parse JSON.
151 | ERROR: ${e.name} ${e.message}
at Module._compile (node_modules/pirates/lib/index.js:135:24)
at json (e2e/runJest.ts:148:11)
at Object.<anonymous> (e2e/__tests__/transform.test.ts:326:30)
● on node >=12.17.0 › transform-esm-testrunner › runs test with native ESM
expect(received).toMatch(expected)
Expected pattern: /PASS/
Received string: "FAIL __tests__/add.test.js
● Test suite failed to run·
/Users/feng/dev/jest/e2e/transform/transform-esm-testrunner/test-runner.mjs:7
import testResult from '@jest/test-result';
^^^^^^·
SyntaxError: Cannot use import statement outside a module·
at Module._compile (../../../node_modules/pirates/lib/index.js:135:24)·
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.258 s
Ran all test suites."
344 | });
345 |
> 346 | expect(stderr).toMatch(/PASS/);
| ^
347 | expect(json.success).toBe(true);
348 | expect(json.numPassedTests).toBe(1);
349 | });
at Object.toMatch (e2e/__tests__/transform.test.ts:346:22)
Test Suites: 1 failed, 1 total
Tests: 2 failed, 20 passed, 22 total
Snapshots: 5 passed, 5 total
Time: 42.588 s, estimated 59 s
Ran all test suites matching /\/Users\/feng\/dev\/jest\/e2e\/__tests__\/transform.test.ts/i.
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.
pirate just hijack require, .mjs
is es module, so we should filter out .mjs
extension when we addHook
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.
if we override Module._extensions[".mjs"]
, then require('test-runner.mjs')
will not throw ERR_REQUIRE_ESM
error but instead will an Cannot use import statement outside a module
error will throw when pirates call Module._compile
.
nodejs source code:
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.
I added some comments to make the filter
mjs extension's intention more clearer.
f40272f
to
7282d99
Compare
8be5ff0
to
be8ad01
Compare
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.
thanks!
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
fix #12237
Test plan
test locally, see below: