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

'There are multiple mobx instances active' error caused by jest mocks #1672

Closed
vonovak opened this issue Aug 9, 2018 · 6 comments
Closed

Comments

@vonovak
Copy link
Contributor

vonovak commented Aug 9, 2018

I think more people will run into this or already did, perhaps we can discuss the solution. This is a question / issue that I have met while upgrading mobx 3.3 to 4.3. It's of course possible this is more a Jest problem rather than MobX, but I'm posting here for better visibility.

I'm using Jest 23 for tests and after the upgrade, when I run the tests, I get:

Error: [mobx] There are multiple mobx instances active. This might lead to unexpected results. See https://github.com/mobxjs/mobx/issues/1082 for details.

I only encounter the problem when running tests. The problem stems from jest.genMockFromModule (and possibly Jest's other mocking mechanisms). It is reproducible by creating a mock of a module that imports from mobx. A minimal repro is here - just clone it, install and run npm t.

Solution

I'm still looking for a proper one. I have tried using

import { configure } from 'mobx';
configure({ isolateGlobalState: true });

in the setup file but it seems like there is some sort of asynchronous behavior involved - it does improve things, but fails with the same error at some point.

Any pointers appreciated. Thanks!

@mweststrate
Copy link
Member

This seems to be caused by jestjs/jest#4940, if I you add a console.trace() to node_modules/mobx/mobx.js you will indeed see that Jest does not cache the module, but includes it twice:

  console.error console.js:188
    [object Object]
        at Object.<anonymous> (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/mobx/lib/mobx.js:2873:13)
        at Runtime._execModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:694:13)
        at Runtime.requireModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:376:14)
        at Runtime.requireModuleOrMock (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:463:19)
        at Object.<anonymous> (/home/michel/Desktop/tmp/mobxMultipleInstances/src/User.js:1:1)
        at Runtime._execModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:694:13)
        at Runtime.requireModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:376:14)
        at Runtime.requireModuleOrMock (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:463:19)
        at Object.<anonymous> (/home/michel/Desktop/tmp/mobxMultipleInstances/src/__tests__/User.unit.test.js:1:1)
        at Runtime._execModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:694:13)

  console.error console.js:188
    [object Object]
        at Object.<anonymous> (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/mobx/lib/mobx.js:2873:13)
        at Runtime._execModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:694:13)
        at Runtime.requireModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:376:14)
        at Runtime.requireModuleOrMock (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:463:19)
        at Object.<anonymous> (/home/michel/Desktop/tmp/mobxMultipleInstances/src/MyClass.js:1:1)
        at Runtime._execModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:694:13)
        at Runtime.requireModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:376:14)
        at Runtime._generateMock (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:739:34)
        at Object.genMockFromModule (/home/michel/Desktop/tmp/mobxMultipleInstances/node_modules/jest-runtime/build/index.js:957:45)
        at Object.genMockFromModule (/home/michel/Desktop/tmp/mobxMultipleInstances/src/__mocks__/MyClass.js:3:26)

@mweststrate
Copy link
Member

You can now upgrade to MobX 4.4 / 5.1, which can work around this error

@EQuimper
Copy link

EQuimper commented Aug 27, 2018

@mweststrate still get this error even after upgrading to version 5.1 I want to unit test a model build with mobx-state-tree and have the same error.
screenshot 2018-08-27 16 51 48

@mweststrate
Copy link
Member

mweststrate commented Aug 27, 2018 via email

@EQuimper
Copy link

@mweststrate I just make it work, in react-native I put globals window: true when it should be an empty object. Sorry for this

@jayshah123
Copy link

This used to be a warning in previous mobx versions, on the latest mobx versions it straight out crashes. I will raise the issue with an example test/demo.

@mobxjs mobxjs locked as resolved and limited conversation to collaborators Mar 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants