SyntaxError: Cannot use import statement outside a module: when running Jest-expo tests
See original GitHub issueđ Bug Report
Summary of Issue
Jest test case throwing the error -> SyntaxError: Cannot use import statement outside a module:
Updated the transformIgnorePatterns
as told in the last ticket but it is throwing the same error. #10996
Environment - output of expo diagnostics
& the platform(s) youâre targeting
Android
Error :
({âObject.â:function(module,exports,require,__dirname,__filename,global,jest){import connectStyle, { clearThemeCache } from â./src/connectStyleâ; ^^^^^^
SyntaxError: Cannot use import statement outside a module
Reproducible Demo
This is to reopen a closed issue. https://github.com/expo/expo/issues/10996
Steps to Reproduce
- Run yarn install
- Run yarn test ( to replicate the test case)
Expected Behavior vs Actual Behavior
The test case shouldnât throw any import related errors.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top Results From Across the Web
SyntaxError: Cannot use import statement outside a module ...
Currently using jest-expo for testing in this project. The version of jest-expo in the project is 39.0.0. Version of Jest installed globally is ......
Read more >SyntaxError: Cannot use import statement outside a module ...
Hi @teja-kandula! It looks like native-base-shoutem-theme contains non-transpiled code, which is fine but not working in Jest. Jest uses babel ...
Read more >TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files...
Read more >jest cannot use import statement outside a module - You.com
Jest doesn't support ES6 module and hence throwing this error when you directly run the test with Jest. if you want to run...
Read more >can not use import statement outside a module - YouTube
JS - JavaScript. syntaxerror - can not use import statement outside a module | es6 vs commonjs modules in nodejs.
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
Thanks for your suggestion. Importing the preset turned out to be part of the solution, but a few more things were needed. Things behaved correctly when we did the following:
baseConfig
babel.config.js
filename in order to avoid conflicts with Expo and NextJS in our projectAfter setup, we now have a working project using
*.jsx
component and test files.I think thereâs an opportunity for this preset-importing tip to be added to Expoâs Jest documentation.
đ For those following this thread, here are copies of our working jest and special babel configs:
jest.config.js and babel-jest.config.js
jest.config.js
babel-jest.config.js (referenced only in jest.config.js)
Hat tip to @marcod1419 for figuring this out. đ
I suspect that the behavior that you are seeing is due to some quirk in the way jest handles merging configs - we donât ingest the developerâs jest config in jest-expo, thatâs all handled by jest.
You can merge configs yourself though because itâs a js file, and then you get more control. Something like this should work: