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.

`SyntaxError: Unexpected token import` when running Jest and importing a React component from nested directory

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
brunomacedocommented, Sep 21, 2018

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

C:\App\projects\contabilidade\fe-contabilidade-client\specs\jest.setup.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';

             ^^^^^

SyntaxError: Unexpected identifier

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
5reactions
bugzpoddercommented, Jun 8, 2018

@Noitidart @JulianeAlbuquerque Please run yarn test or npm run test instead of jest

Read more comments on GitHub >

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

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