-
-
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
[babel-jest] mjs files #4637
Comments
Another way expect from transpiring with babel would be to use experimental ES6 module support, see https://nodejs.org/dist/latest-v8.x/docs/api/esm.html I am currently trying to extend the testMatchers like this:
but still my test named [test].test.mjs is still not found. |
@mknet , So not fully understating it, I used a plain stupid approach with:
and that (probably not even surprisingly to me) worked. We also need to specify But still, with all that it fails to me with the error similar to the one @elegos reports :-(. Wish there were better support of ESM in Jest. Because ESM's are great, I believe. |
So to clarify, this still doesn't work from what I can tell. |
@kirlat |
@elegos You need to add |
Came here because I added a transform to my package.json to transpile files with the .es6 label. It didn't work until also adding ".es6" to moduleFileExtensions
|
The get-around as of jest 22.4.3 and babel-core 6.26.3:
Therefore insert the following into package.json:
add preset: yarn test now works with tests named .test.mjs and source files named .mjs |
thank you @haraldrudell your solution saved me a lot of hairpulling. Your solution also works with Babel7 once plugin names are adjusted.
{
"env": {
"test": {
"presets": [["@babel/preset-env",{"modules": false}]],
"plugins": [["@babel/plugin-transform-modules-commonjs", {"spec": true}]]
}
}
}
|
Isn't this a dupe of #4842? @haraldrudell: can you please format that package.json code as a code block, using three backticks? Also, I get a syntax error due to the @kirlat: that glob pattern is confusing because you were expecting a regular expression, but |
In my case all I needed to do to get it working was add
(Of course if you don't already have the |
Comment above is correct 🙂 |
@mbrowne this solution was already mentioned in #4637 (comment) @SimenB: the problem isn't entirely solved. I'm using the settings listed by @mbrowne, but get weird errors with imports from CommonJS modules. Here's a reproduction repo: https://github.com/dandv/jest-mjs-devx Please re-open. Using |
PR most definitely for documenting mjs usage. Thanks for the reproduction, I'll take a look later today! |
@dandv this is a problem with graphql-tools - they have transpiled away their This fixes your test, but makes diff --git i/basic.test.mjs w/basic.test.mjs
index 972110c..427162c 100644
--- i/basic.test.mjs
+++ w/basic.test.mjs
@@ -1,5 +1,5 @@
import { makeSchema } from './lib';
test('basic test', () => {
- expect(makeSchema).toNotBe(null);
+ expect(makeSchema).not.toBe(null);
});
diff --git i/lib.mjs w/lib.mjs
index 5717d51..81e22e0 100644
--- i/lib.mjs
+++ w/lib.mjs
@@ -1,5 +1,6 @@
-import graphQLTools from 'graphql-tools';
-export const makeSchema = graphQLTools.makeExecutableSchema({
+import { makeExecutableSchema } from 'graphql-tools';
+
+export const makeSchema = makeExecutableSchema({
typeDefs: 'type Foo { bar: Int! }',
resolvers: {},
}); The way of importing I changed to is the way it's documented here: https://github.com/apollographql/graphql-tools. The fact that doesn't work with Node's esm mode is not Jest's fault |
Also note that the current implementation in node might be pulled and redone, so asking the ecosystem to change so you can use experimental flags with their current behavior might not be the best way forward: nodejs/modules#180 Something like |
it isn't getting pulled, but we are removing some of the features. Edit: to be clear I meant not going anywhere as in it is not going to disappear, we believe it will stick around for all future implementations |
@SimenB: any idea how to actually get @std/esm to work with Jest? |
See #7018 for latest progress on that, although that proposal will not work alongside babel-jest |
Indeed, v12 released new experimental support for |
nodejs/node#27387 may be of interest |
I have a node_module (Ramda) that is building to a Is there a way to fix this issue with Babel? |
Here's what I did in my
|
Any updates now that ES modules are no longer behind an experimental flag? |
None of this works..
app.test.mjs
app.jsx
jest.config.js
babel.config.js
package.json
no luck still above error |
Thanks I see now that I missed the transform part, I changed it to
or (more correctly?):
I still get:
or
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
babel-jest is not able to transpile es6 module statements if in *.mjs files
If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can
yarn install
andyarn test
.https://repl.it/MU09/0
What is the expected behavior?
Babel should transpile module import/export statements also with mjs files
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
jest.config.js
.babelrc
Note: I've tried disabling the transform-es2015-modules-commonjs plugin, but then the syntax error is back in the test js file
Node: v8.6
babel-core: 6.26.0
babel-jest: 21.2.0
babel-plugin-transform-es2015-modules-commonjs: 6.24.1
babel-preset-es2015: 6.24.1
jest: 21.2.0
Example output error:
P.S.
If I call my test files ending with
.test.mjs
and I edit thetestMatch
accordingly, jest won't recognize themThe text was updated successfully, but these errors were encountered: