React-testing-library: Cannot find module 'react-dom/test-utils' from 'act-compat.js'

Created on 6 Mar 2019  路  9Comments  路  Source: testing-library/react-testing-library

  • react-testing-library version: 6.0.0
  • react version: 15.4.1
  • node version: 8.11.3
  • npm (or yarn) version: 1.12.3

Relevant code or config:

import 'react-testing-library/cleanup-after-each';

What you did:

Just imported the react-testing-library/cleanup-after-each

What happened:

Got this error Cannot find module 'react-dom/test-utils' from 'act-compat.js'

Reproduction:

Problem description:

The react-addons-test-utils was moved to react-dom/test-utils in the version 15.5, that path doesn't work in react-dom <15.5

Suggested solution:

Import from react-addons-test-utils or react-dom/lib/ReactTestUtils for react-dom 15.4.x versions, see this line
https://github.com/kentcdodds/react-testing-library/blob/master/src/act-compat.js#L3

released

Most helpful comment

:tada: This issue has been resolved in version 6.0.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

All 9 comments

Hmmm... This is tricky because we generate a UMD build so we can't conditionally import based on your version of react. What do you think we could do?

Seeing this issue as well @merraysy

@kentcdodds Here are my configs:

react - 15.3.1
react-dom - 15.3.1
react-testing-library - 6.0.0
jest - 23.6.0

Is react-testing-library not compatible with my version of React?

I'm seeing this issue even though my project has a dependency on React 16.3 - apparently, one of the project's other dependencies has a dependency on React 15.2 and jest-resolve is getting confused? Anyone see anything similar?

I see this bug with React 0.14.7 (upgrading not currently an option). I worked around it by replacing the import on https://github.com/kentcdodds/react-testing-library/blob/master/src/act-compat.js#L3 with the code below. It seems to build fine although the catch block is not covered by tests - if this approach is good enough for a PR maybe someone can find a way to test it, my efforts failed and /* istanbul ignore next */ doesn't work for some reason.

let reactAct

try {
  reactAct = require('react-dom/test-utils').default
} catch (e) { 
  reactAct = require('react-addons-test-utils').default
}

:tada: This issue has been resolved in version 6.0.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

@kentcdodds The issue reappeared again v9.4.0 same react version.

What do you think we could do?

Sorry I don't know how I've completely forgotten about this issue and never looked back :)

Sorry about that. In thinking about this further, I'm afraid that the old approach we took will not work for all cases. So I'm going to suggest that you mock that module. I think if you put this in your setupFilesAfterEnv it should work without trouble:

jest.mock('react-dom/test-utils', () => ({}))

Good luck!

@kentcdodds I get the same issue after replace React to Preact.

For those who lands here after switching from React to Preact: react-testing-library -> preact-testing-library

Was this page helpful?
0 / 5 - 0 ratings