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 fails on standard import syntax

See original GitHub issue

When 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:open
  • Created 9 months ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
clemengercommented, Dec 21, 2022

@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:

Failed to compile.

Attempted import error: 'Tooltip' is not exported from 'react-tooltip/dist/react-tooltip.umd' (imported as 'Tooltip').
ERROR in ./src/App.js 45:37-44
export 'Tooltip' (imported as 'Tooltip') was not found in 'react-tooltip/dist/react-tooltip.umd' (module has no exports)

webpack compiled with 1 error
1reaction
danielbarioncommented, Dec 20, 2022

Thanks for the fast reply with the example, we will take a look as soon as possible and let you know our findings!

Read more comments on GitHub >

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

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