question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest encountered an unexpected token - "import *"

See original GitHub issue

Hi, 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:closed
  • Created 3 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
jlarmstrongivcommented, Sep 18, 2021

That’s a great tip! Workaround for create-react-app in package.json:

{
  "jest": {
    "moduleNameMapper": {
      "react-pdf/dist/esm/entry.webpack": "react-pdf",
    }
  }
}
2reactions
madelineurlcommented, Aug 18, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found