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.

Unable to run Jest tests when running under create-react-app

See original GitHub issue

When trying to run Jest tests under create react app with yarn test I get the following error:

  ● Test suite failed to run

    /projects/project/node_modules/jsoneditor-react/es/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React, { Component } from 'react';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      2 | import { Button, Col, Row, Container } from "reactstrap";
      3 |
    > 4 | import { JsonEditor as Editor } from "jsoneditor-react";
        | ^
      5 | import "jsoneditor-react/es/editor.min.css";
      6 |

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)

The code compiles just fine without any errors and works as intended. Somehow running it with NODE_ENV=test causes the transpiler to fail.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
bruderdacommented, Jun 10, 2021

@lepinkainen here’s what I used:

package.json:

  "jest": {
    "moduleNameMapper": {
      "jsoneditor-react": "<rootDir>/src/stub.js"
    }
  },

stub.js:

var Editor = function() {};
export { Editor as JsonEditor };
1reaction
alicialicscommented, Jul 8, 2020

transformIgnorePatterns did not work for me.

Had to use: moduleNameMapper: { “jsoneditor-react”: a file with empty stub }

Read more comments on GitHub >

github_iconTop Results From Across the Web

'Failed to initialize watch plugin' when running tests for newly ...
Describe the bug Running tests (using npm test) for a newly created app throws the error: Error: Failed to initialize watch plugin ...
Read more >
Running Tests | Create React App
Jest is a Node-based runner. This means that the tests always run in a Node environment and not in a real browser.
Read more >
Jest test not passing in create react app - Stack Overflow
My create-react-app (es6) with redux builds and runs in the browser as expected. I think there is something wrong with my imports in...
Read more >
Testing Environments - React
This document goes through the factors that can affect your environment and recommendations for some scenarios. Test runners. Test runners like Jest, mocha, ......
Read more >
Cannot run Jest tests with create-react-app fork - YouTrack
First, I thought that it was due to one of our customizations. So I took the plain create-react-app source and just published the...
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