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.

Build issue during Jest tests when importing react-children-utilities

See original GitHub issue

Describe the bug Importing any file from react-children-utilities results in a build error when running the jest test suite.

    D:\new-create-react-app\node_modules\react-children-utilities\react-children-utilities.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { Children } from 'react';
                                                                                                    ^

    SyntaxError: Unexpected token {

      3 | import './App.css';
      4 |
    > 5 | import { deepMap } from 'react-children-utilities';
        | ^
      6 |
      7 | function App() {
      8 |   return (

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/App.js:5:1)

I am unclear whether this issue exists in react-children-utilities or whatever it is a configuration problem with jest, but a default installation of create-react-app also results in this issue. Builds etc are fine, only the jest test is broken.

There are various tickets https://github.com/facebook/jest/issues/6229 discussing this problem, however none of the suggestions work for this particular library.

To Reproduce Steps to reproduce the behavior:

  1. Install a new project from https://github.com/facebook/create-react-app (npx create-react-app my-app)
  2. Eject the application (yarn eject)
  3. At this point running yarn run test should pass all tests
  4. yarn add react-children-utilities
  5. Update /src/App.js to import anything from react-children-utilities, for example: import { deepMap } from 'react-children-utilities';
  6. At this point running yarn run test will fail with the above error.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
deepakjha14commented, Dec 26, 2019

This issues still persist, i have run into this problem and spent couple of days already in search of the answer. I have i guess tried all the possible options given on this page and other github pages, no luck 😦.

2reactions
fernandopasikcommented, Dec 9, 2019

Yes, after doing the yarn eject it will be there.

OK, I tried again with create-react-app

I did two things.

1 - Added the ignore in package.json jest config

"transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\](?!react-children-utilities).+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
],

2 - Created a babel.config.js in the root folder with the content

module.exports = {
  presets: ["react-app"]
};

Those two steps made the tests pass

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test Utilities - React
ReactTestUtils makes it easy to test React components in the testing framework of your choice. At Facebook we use Jest for painless JavaScript...
Read more >
Continuous integration for React applications using Jest and ...
First, you need to import the App component, but the only export in App.js is the Redux -connected version of the component: export...
Read more >
React & Jest, how to test changing state and checking for ...
Figured it out! Did not need React Test Utilities it('should render the Notification component if state.error is true', ...
Read more >
React app testing: Jest and React Testing Library
Here, the test case is provided by Jest. For rendering and accessing the virtual DOM, we import and use both render and screen...
Read more >
React Unit Testing Using Enzyme and Jest - Toptal
Therefore, one of the best ways to avoid creating legacy code is using test-driven development (TDD). While there are many tools available 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