Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lepinkainen opened this issue May 25, 2020 · 6 comments
Open

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

lepinkainen opened this issue May 25, 2020 · 6 comments

Comments

@lepinkainen
Copy link

lepinkainen commented May 25, 2020

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.

@vankop
Copy link
Owner

vankop commented May 25, 2020

you need to provide correct transformer to jest, see https://jestjs.io/docs/en/getting-started#using-babel

@lepinkainen
Copy link
Author

It's a bit of a pain to edit the babel settings in a CRA based project without ejecting it.

Any hints on what are the correct transformers to make it work in a CRA TypeScript project? Or why this error is thrown during testing, but not when compiling?

@vankop
Copy link
Owner

vankop commented May 25, 2020

@lepinkainen if you will create reproduction repo I could take a look. As I understand you can override options
https://create-react-app.dev/docs/running-tests#configuration
you need to allow jsoneditor-react https://jestjs.io/docs/en/configuration#transformignorepatterns-arraystring

@alicialics
Copy link

alicialics commented Jul 8, 2020

transformIgnorePatterns did not work for me.

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

@lepinkainen
Copy link
Author

@alicialics Can you share the stub file you used?

@bruderda
Copy link

@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 };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants