Jest encountered an unexpected token - "import *"
See original GitHub issueHi, while trying to run tests, I’m getting an issue. I tried solutions including installing multiple babel packages, configuring them in packages.json, adding .babelrc files, but nothing seems to work.
Jest complains, that the webpack.js from the react-pdf package is not a correct javascript file. Has anyone else had this issue or knows a workaround for the issue?
Error Message: Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
C:\Code\harmo\my-application\node_modules\react-pdf\dist\esm\entry.webpack.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import * as pdfjs from 'pdfjs-dist';
^`
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
jest: Test suite failed to run, SyntaxError: Unexpected token ...
Jest sets the env variable to test, so I had to add my presets to the env setting in .babelrc: { "plugins": ["syntax-dynamic-import", ......
Read more >How I Fixed The Unexpected Token Error In Jest
Jest encountered an unexpected token. Depending upon your setup, you might see the error on the first line of the code file or...
Read more >Unexpected Token Import for ES6 modules #2081 - GitHub
It seems that Jest is missing the babel configuration in my package.json and the test suite is failing with 'Unexpected Token Import'.
Read more >How I resolved issues while setting up Jest and Enzyme in a ...
Jest encountered an unexpected token. This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not...
Read more >jest encountered an unexpected token ts-jest - You.com
Test suite failed to run Jest encountered an unexpected token This usually means that you are trying to import a file which Jest...
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 FreeTop 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
Top GitHub Comments
That’s a great tip! Workaround for
create-react-app
inpackage.json
:Also having this issue, but only when importing pdfs from ‘react-pdf-dist/esm/entry.webpack’ for performance as instructed in the documentation. Importing PDF and Document directly from ‘react-pdf’ works in Jest, but fails in the browser. Vice versa for importing as instructed in the docs