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.

create-react-app: Unexpected token in App.test.js

See original GitHub issue

Environment

  1. node -v: v8.11.1

  2. npm -v: 5.6.0

  3. npm ls react-scripts (if you haven’t ejected): react-scripts@1.1.4

  4. Operating system: [MacOS 10.13.5]

Steps to Reproduce

$ create-react-app test

open up workspace with extension installed In the output for the test (next to terminal/debug console/problems) see output:

 FAIL  src/App/App.test.js
  ● Test suite failed to run

    /path/to/project/App/App.test.js: Unexpected token (7:18)
         5 | it('renders without crashing', () => {
         6 |   const div = document.createElement('div');
      >  7 |   ReactDOM.render(<App />, div);
           |                   ^
         8 |   ReactDOM.unmountComponentAtNode(div);
         9 | });
        10 | 


Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.157s
Ran all test suites related to changed files.

Expected Behavior

Jest test passes when run by extension

Actual Behavior

Jest tests fail when run by extension

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:20
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
Royal-Crown-Colacommented, Sep 21, 2019

This issue still exists as of 9/21/19

3reactions
naholyrcommented, Jul 18, 2018

I think I found a temporary workaround:

  • copy relevant configuration from ejected package.json (keys “jest” and “babel”)
  • edit paths by replacing “<rootDir>/config” to “<rootDir>/node_modules/react-scripts/config”
  • edit “testEnvironment” to “jsdom” to fix undefined documents

Now it’s running properly for me. Here is what I finally added to my package.json:

{
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,mjs}"
    ],
    "setupFiles": [
      "<rootDir>/node_modules/react-scripts/config/polyfills.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
    ],
    "testEnvironment": "jsdom",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|mjs)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/node_modules/react-scripts/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/node_modules/react-scripts/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "node",
      "mjs"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  }
}

I’m not sure everything is required, but it’s juste copy-pasta from ejected project, I didn’t want to bother fine-tuning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create-React-App build - "Uncaught SyntaxError: Unexpected ...
When I deploy without changing any default settings, I will get this "Uncaught SyntaxError: Unexpected token <" error and nothing shows up on ......
Read more >
Fix "Jest encountered an unexpected token" with "create-react ...
Here are a few ways I have seen, assume the package is being used @fullcalendar : Use a customized package react app customize-cra...
Read more >
SyntaxError: Unexpected token '<' on new build of React
I have a React application and every time I deploy it to digital ocean I'm getting this error SyntaxError: Unexpected token '<' ,...
Read more >
How To Fix Unexpected Token Error for NPM Package in react ...
Are you getting this error with react -leaflet or other packages. If you are then it's most likely related to babel and there...
Read more >
[Solved]-Jest with Create React App: Test suite failed to run
[Solved]-Jest with Create React App: Test suite failed to run - Unexpected token-Reactjs ... These configs you can find after you do npm...
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