fix: Cannot use import statement outside a module
See original GitHub issue🐛 The bug
Since I upgraded to 0.33.0
, my tests are not passing anymore because it seems like Jest is pointing to the wrong file.
node_modules/@nuxtjs/composition-api/dist/runtime/index.mjs
instead of node_modules/@nuxtjs/composition-api/dist/runtime/index.js
Full output
FAIL bddf-bodyguard my-project/test/integration/components/MyComponentTest.test.ts
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/my-project/node_modules/.pnpm/@nuxtjs+composition-api@0.33.0_ndg5g6d2rkyeqt4q3tlrv66tjm/node_modules/@nuxtjs/composition-api/dist/runtime/index.mjs:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { getCurrentInstance as getCurrentInstance$1, ref, customRef, shallowRef, onServerPrefetch, isRef, reactive, toRefs, watch, computed, isReactive, toRaw, onBeforeMount, set } from 'vue';
^^^^^^
SyntaxError: Cannot use import statement outside a module
[...]
at Runtime.createScriptFromCode (../../node_modules/.pnpm/jest-runtime@28.1.3/node_modules/jest-runtime/build/index.js:1796:14)
at Object.<anonymous> (components/MyComponent.vue:9:27)
at Object.<anonymous> (test/integration/components/MyComponentTest.test.ts:6:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 8.58 s
Ran all test suites.
🛠️ To reproduce Steps to reproduce the behavior:
- Create a new Nuxt 2 project
- Configure Jest
- Install
@nuxtjs/composition-api@0.33.0
- Try to launch your tests
Repro here: https://stackblitz.com/edit/nuxt-starter-btq2rq?file=jest.config.ts
🌈 Expected behaviour Tests pass
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
"Uncaught SyntaxError: Cannot use import statement outside ...
There are three ways to solve this:- · 1. The first: In the script, include type=module · 2. The second: In node.js, into...
Read more >Cannot use import statement outside a module [React ...
In this article, we talked about the SyntaxError: Cannot use import statement outside a module error in TypeScript and JavaScript. This error ...
Read more >How to fix "cannot use import statement outside a module"
I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript file. This...
Read more >How to solve: cannot use import statement outside a module
How to solve: cannot use import statement outside a module · 1. Transpile your code · 2. Set "type" to "module" in package.json...
Read more >Javascript Fix Cannot Use Import Statement Outside A Module
STEP #1: Replicate Cannot Use Import Statement Outside A Module Error · STEP #2: Add Type Module Attribute To The Script Tags ·...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Whoops yeah, I’m working on an Nx repo and I forgot that my node_modules is not directly located in the project itself, it works now, thanks, you rock 🎸
Hi @danielroe, after updating to
0.33.1
, I got also errorSyntaxError: Cannot use import statement outside a module
. So I addednode_modules/@nuxtjs/composition-api/dist/runtime/index.js
to jest.config.js. But now I get this error:Any idea?
My jest.config.js
my package.json
my tsconfig