Jest fails on standard import syntax
See original GitHub issueWhen importing Tooltip, Jest throws the following error unless you specifically import the cjs version. react-tooltip v5.2.0 To reproduce, in a project running Node 18, React 18, and Jest 29, import tooltip:
import { Tooltip as ReactTooltip, ITooltip } from 'react-tooltip';
Project runs fine, but jest tests fail.
When running jest tests I see the following error:
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.
...node_modules/react-tooltip/dist/react-tooltip.esm.min.js:1
So, it’s possible it’s just my jest configuration that is unable to support this syntax, but the tests run fine with standard imports from other react ui packages.
Current solution: directly import from the dist directory’s .cjs version.
import { Tooltip as ReactTooltip, ITooltip } from '../../node_modules/react-tooltip/dist/react-tooltip.cjs';
Issue Analytics
- State:
- Created 9 months ago
- Reactions:3
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Jest fails with "Unexpected token *" on import statement
You will need to tell Jest to compile react-navigation-tabs by whitelisting it in the transformIgnorePatterns option in your Jest config.
Read more >Jest tests can't process import statement #1584 - vuejs/vue-cli
I created a new app with vue-cli, selecting Jest tests. ... failing with error: SyntaxError: Cannot use import statement outside a module ......
Read more >Configuring Jest
Jest will fail if: The ./src/components directory has less than 40% branch or statement coverage. One of the files matching the .
Read more >jest encountered an unexpected token jest failed to parse a ...
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...
Read more >TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when jest is misconfigured in a TypeScript project. To solve the error,...
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
@danielbarion yes, the jest test worked for me with the umd file. However, similar to the attempted workaround with the .cjs file, the .umd file works for jest, but if you run
yarn start
in the test project, it gives the following compile error with the umd file:Thanks for the fast reply with the example, we will take a look as soon as possible and let you know our findings!