Tests failing with SyntaxError on import statements
See original GitHub issue- Issue
After configuring ts-jest all my tests fail with SyntaxError on import statements, even if they didn’t include any typescript modules and were passing before. If I remove the ts-jest config, the tests that don’t import a typescript module passes fine.
...\sum.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Sum from './sum';
^^^^^^
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules\jest-runtime\build\transform.js:320:12)
Random thought as I post this, all our tests are written in js and so are most of our components. We have a handful of shared components that are written in typescript. ts-jest should work for this scenario right?
- Expected behavior
Tests that run/pass before configuring ts-jest continue to do so and tests typscript module passes too
- Link to a minimal repo that reproduces this issue
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top Results From Across the Web
SyntaxError: Cannot use import statement outside a module ...
This is a React, Typescript, Webpack project. I am trying to test a module. But Jest won't transform the module to plain javascript...
Read more >SyntaxError: Cannot use import statement outside a module
I ran 'yarn rw test' and i output from the testing. called “SyntaxError: Cannot use import statement outside a module”.
Read more >jest cannot use import statement outside a module - You.com
I'm trying to set up unit testing in Vue using Jest. However, I'm getting this error: Test suite failed to run ... SyntaxError:...
Read more >Jest vs “import statement outside a module” - David Votrubec
As the name suggests, lodash-es uses ES6 modules syntax. Jest does not like it, and the unit tests will fail with syntax error....
Read more >Cannot use import statement outside a module (phpStorm/Js ...
SyntaxError: Cannot use import statement outside a module (phpStorm/Js/Jest) Follow ... I'm trying to setup phpStorm to run javascript unit-test ...
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

@kwilkins The js files need to be transpiled for node to understand
import. There are two ways you can do this:let me know if this fixes it for you
sidenode: the jest/ts-jest version in the linked repo is too old
I am still getting the “Unexpected token import” after updating the transform to include js files (my tests are js but my components are ts/tsx)
Example failure: ` Test suite failed to run