Skip to content
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

[Bug]: Cannot use import statement outside a module #12990

Closed
thany opened this issue Jul 5, 2022 · 8 comments
Closed

[Bug]: Cannot use import statement outside a module #12990

thany opened this issue Jul 5, 2022 · 8 comments

Comments

@thany
Copy link

thany commented Jul 5, 2022

Version

28.1.2

Steps to reproduce

  1. Install jest & babel-jest
  2. Add "targets": { "node": "current" } to the options for @babel/preset-env in the babel.config.json
  3. Add transform: { "\\.js$": "babel-jest" } to the jest.config.js
  4. Add any test that contains import X from X at the top
  5. Run jest

Expected behavior

According to the documentation on how to deal with code that is 4 years old, i.e. contains import, it should now work.

I would actually expect it to Just Work without any additional config. I mean, how can import not work? It works natively in Node.js absolutely fine, so why wouldn't it in Jest??

Actual behavior

This is a relevant excerpt from the error message:

    C:\Projects\<blah>\options.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { parseScriptTags } from './options';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)

Additional context

I also can prove that the babel-jest transform produces code that still contains that top-level import that Jest doesn't seem to like. So the problem (and perhaps the solution) might be in the babel-jest package.

But more to the point, it should just work without needing any configuration. It's 2022 and this should've worked in 2018 without any config. Everything I "have to" configure to get this stuff to work, can be auto-detected. Or there should be more practical defaults to ship in Jest itself, that allow it to deal with modern code.

Environment

System:
    OS: Windows 10 10.0.19044
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 16.15.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.13.2 - C:\Program Files\nodejs\npm.CMD
@thany
Copy link
Author

thany commented Jul 5, 2022

Or alternatively, if the error message itself is accurate, then why is my options.test.js not being treated as a "module"? If it so desperately wants it to be a module, then fine - make it a module, I don't give a fluff. As long as the tests execute, right?

@thany
Copy link
Author

thany commented Jul 6, 2022

Same problem with await, even though it doesn't complain on async...

SyntaxError: C:\<blah>\card.test.js: Unexpected reserved word 'await'. (19:4)

      17 |     });
      18 |
    > 19 |     await component.setProps({ msg: 'foo' })
         |     ^
      20 |
      21 |     expect(component.html()).toContain('<p>foo</p>');
      22 |   });

No bit of the documentation explains what to do to get Jest to understand this syntax, yet this part just willy-nilly assumes you've got some kind of magical configuration set up (that, again, is not mentioned anywhere) that makes Jest understand async/await. Very disappointing.

@markcellus
Copy link

@thany not sure if this helps, but setting experimental-vm-modules before calling jest fixed issue for me:

NODE_OPTIONS=--experimental-vm-modules npx jest

Got it from this page. Hope this helps

@thany
Copy link
Author

thany commented Jul 15, 2022

Yes, I know, but it's 2022 and there's nothing experimental about this syntax. It should just work by itself.

Edit: to clarify, the goal of this issue is not intended to get my tests to run. It got it. The intent is to actually fix this problem once and for all. It's well overdue, iyam.

@markcellus
Copy link

Yeah, they're already working on it. See #9430.

the goal of this issue is not intended to get my tests to run.

Might be best to close this issue and add your support in 9430 above then. Good luck!

@thany
Copy link
Author

thany commented Aug 1, 2022

That issue dates from 2.5 years ago. Can I just ask how such a thing is taking that much time?

I'm not impatient or anything, but 2.5 years is a long time...

@SimenB
Copy link
Member

SimenB commented Aug 1, 2022

Feel free to follow nodejs/node#37648 and/or put in the work necessary in v8 in order for node to stabilise the API Jest uses. There is nothing jest can do here without the underlying platform moving forward.

https://jestjs.io/docs/ecmascript-modules has instructions for how to use native ESM today.

@SimenB SimenB closed this as not planned Won't fix, can't repro, duplicate, stale Aug 1, 2022
@github-actions
Copy link

github-actions bot commented Sep 1, 2022

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants