`SyntaxError: Unexpected token import` when running Jest and importing a React component from nested directory
See original GitHub issueI am using create-react-app and having trouble running Jest.
When I attempt to import a React component into my test file, I get an error when I yarn test
:
Test suite failed to run
.../src/node_modules/common/ErrorMessage.js:1
({"Object.<anonymous>":function(
module,exports,require,__dirname,__filename,global,jest)
{import React from 'react'
^^^^^^
SyntaxError: Unexpected token import
This is what my test file looks like:
// test.test.js
// attempting to import
import ErrorMessage from '../node_modules/common/ErrorMessage.js'
// dummy test
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3)
})
However, the error does not get thrown if I’m at the root of the src
file, importing my index.js
. At that point, the error gets thrown in the first file that index.js
imports. For example:
test.test.js
import index from './index'
index.js
import React from 'react'
import { render } from 'react-dom'
import './style/index.css'
import LoginContainer from './node_modules/user/LoginContainer'
import NewUser from './node_modules/user/NewUser'
// etc.
terminal output
FAIL src/test.test.js
● Test suite failed to run
/Users/hannahmccain/Desktop/DEV/expense-report/fullstack-expense-report/client/src/node_modules/user/LoginContainer.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React, { Component } from 'react'
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
at Object.<anonymous> (src/index.js:11:164)
at Object.<anonymous> (src/test.test.js:3:14)
It seems like, in the context of Jest, Babel isn’t able to compile the files in src/node_modules
properly. I’m just at a loss as to how to correct that! Any insights would be appreciated.
Please let me know if you need more code, context, or info! I’m new at this 😃
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (1 by maintainers)
Top Results From Across the Web
Jest - react-native SyntaxError: Unexpected token import
I am running jest test in react native and it fails giving "SyntaxError: Unexpected token import". Here is my trimmer import code: -...
Read more >jest-worker unexpected token - You.com | The AI Search ...
Issue : ; Jest encountered an unexpected token ; This usually means that you are trying to import a file which Jest cannot...
Read more >[Solved]-Dynamic imports in Jest produce: SyntaxError
Coding example for the question Dynamic imports in Jest produce: SyntaxError: Unexpected token import-Reactjs.
Read more >Troubleshooting | React Navigation
Sometimes it might even be due to a corrupt installation. If clearing cache didn't work, try deleting your node_modules folder and run npm...
Read more >Creating a React Component Library using Rollup, Typescript ...
This component library would enable teams to pull down an NPM package, import components, provide some props and have their components ready to ......
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
Hey guys… Are there another issue for that? What am I doing wrong? It was working well yesterday, but I tried it again now and I got this >>
FAIL specs/Login.spec.js ● Test suite failed to run
@Noitidart @JulianeAlbuquerque Please run yarn test or npm run test instead of
jest