mobxjs / mobx-react

React bindings for MobX
https://mobx.js.org/react-integration.html
MIT License
4.85k stars 349 forks source link

React Native + Jest: Cannot find module 'react-dom' from 'mobxreact.cjs.development.js' #844

Closed jayshah123 closed 4 years ago

jayshah123 commented 4 years ago

Having a fairly barebones react-native + mobx setup here (https://github.com/jayshah123/mobx-jest-test-error) Running npm test asks forreact-dom dependency.

Intended outcome

Jest test should run as expected

Actual outcome

Test fails saying: Cannot find module 'react-dom' from 'mobxreact.cjs.development.js'

Full error:

bash-3.2$ npm test

> JestMobxChecker@0.0.1 test /Users/jay.shah/Documents/sampleapps/JestMobxChecker
> jest

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

    Cannot find module 'react-dom' from 'mobxreact.cjs.development.js'

    However, Jest was able to find:
        './ColorComponent.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    However, Jest was able to find:
        '../App.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:276:11)
      at Object.<anonymous> (node_modules/mobx-react/dist/mobxreact.cjs.development.js:10:16)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.401s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

How to reproduce the issue

https://github.com/jayshah123/mobx-jest-test-error

  1. clone the repo
  2. npm install
  3. npm test

Versions

    "mobx": "^5.15.4",
    "mobx-react": "^6.1.8",
    "react": "16.9.0",
    "react-native": "0.61.5"
danielkcz commented 4 years ago

This will be generally solved by https://github.com/mobxjs/mobx-react/pull/787 once I get to finishing it up :)

For now, you basically have to force mobx-react in Jest config to resolve to RN bundle instead of regular CJS one.

Hm, that might not actually work because RN bundle uses ESM which Jest cannot handle afaik. Cannot think of any viable workaround for now so I guess it makes #787 fairly important now :)

vonovak commented 4 years ago

@jayshah123 this PR makes the test run https://github.com/jayshah123/mobx-jest-test-error/pull/1 . I didn't go any further than that though, so not sure if some more complicated tests relying on mobx will work too.)

danielkcz commented 4 years ago

Please, upgrade to mobx-react 6.2.0 and see instructions in the readme.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.