Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Error when sending a mobx store in a Jest snapshot test #186

Closed
hybrisCole opened this issue Dec 23, 2016 · 14 comments
Closed

Error when sending a mobx store in a Jest snapshot test #186

hybrisCole opened this issue Dec 23, 2016 · 14 comments

Comments

@hybrisCole
Copy link

hybrisCole commented Dec 23, 2016

"mobx": "2.7.0",
"mobx-react": "4.0.3",
"react": "~15.3.2",
"react-native": "0.33.1",

const messageState = require('../../../../stores/message/').default;
messageState.mapThreadData([sampleMessage], '585c990579d78b11a4f97ad1');

test('MessageListView', () => {
  const tree = ReactTestRenderer.create(
    <MessageListView messageState={messageState} />
  ).toJSON();
  expect(tree).toMatchSnapshot();
});

The following throws:
Invariant Violation: ReactUpdates: must inject a reconcile transaction class and batching strategy

But, if I do

test('MessageListView', () => {
  const tree = ReactTestRenderer.create(
    <MessageListView messageState={{
    threads : [{
      id : '585c990579d78b11a4f97ad1',
      messages : [sampleMessage],
    }],
    threadMessages : [sampleMessage],
  }} />
  ).toJSON();
  expect(tree).toMatchSnapshot();
});

It won't fire errors, any ideas?

@mweststrate
Copy link
Member

There is probably a cleaner solution by using a difference test renderer or something, but this might help: https://medium.com/@wietsevenema/using-jest-with-react-native-and-mobx-34949ea7d2cf#.v11m3rh84

Apologies for the late response!

@mweststrate
Copy link
Member

@cpojer does the exception Invariant Violation: ReactUpdates: must inject a reconcile transaction class and batching strategy ring a bell? Searched for it but couldn't find anything tangible. Mobx-react uses reactNative.unstable_batchedUpdates internally, could that give potential issues with the test-renderer?

@cpojer
Copy link

cpojer commented Jan 8, 2017

yeah might be an issue with the test renderer; it's best to bring this up with the react team.

@winterbe
Copy link

winterbe commented Jan 23, 2017

@mweststrate @cpojer I'm facing the same problem after updating my ReactNative apps to Mobx 3. The linked medium article whatever solves the error by adding the following:

jest.mock('mobx-react/native', () => require('mobx-react/custom'));

This works great with Jest watch mode but when using Jest without watch the process hangs forever after executing all tests.

Here's a small open source project to reproduce the issue: https://github.com/winterbe/RNTimerExample

Running npm test on this project results in the following output but the process hangs forever at the last line:

❯ npm test

> [email protected] test /Users/winterbe/Projects/RNTimerExample
> jest

 PASS  test/App.test.js
  ✓ renders correctly (115ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        0.992s, estimated 1s
Ran all test suites.

@cpojer
Copy link

cpojer commented Jan 23, 2017

This means that you aren't properly disabling and unsubscribing from your resources, such as network requests or database connections. Clean up after yourself and it'll work properly!

@winterbe
Copy link

@cpojer Thanks for your comment. However the only test is a simple Jest snapshot test:

jest.mock('mobx-react/native', () => require('mobx-react/custom'));

it('renders correctly', () => {
    const component = renderer.create(
        <App />
    );
    const tree = component.toJSON();
    expect(tree).toMatchSnapshot();
});

I guess it's something Mobx related, e.g. observer reactions not properly closed!? @mweststrate

@cpojer
Copy link

cpojer commented Jan 24, 2017

Yep, seems like that is MobX then. Jest has an escape hatch with --forceExit but we encourage people to fix the underlying issues.

@mweststrate
Copy link
Member

mweststrate commented Jan 24, 2017 via email

@cpojer
Copy link

cpojer commented Jan 24, 2017

It's also possible that this particular component that is rendered sets up an interval or something like that.

@mweststrate
Copy link
Member

mweststrate commented Jan 24, 2017 via email

@winterbe
Copy link

@mweststrate IIRC it's the reaction scheduler which is not disposed at the end of the test suite. When using the following code before test, the message gets logged indefinitely:

jest.mock('mobx-react/native', () => require('mobx-react/custom'));
mobx.extras.setReactionScheduler(() => console.log("scheduling"));

@winterbe
Copy link

winterbe commented Jan 24, 2017 via email

@mweststrate
Copy link
Member

mweststrate commented Jan 24, 2017 via email

@winterbe
Copy link

OK, it was my fault. Sorry for the inconvenience. I have to unmount the component manually for the test renderer to call componentWillUnmount which in my case has side effects to a mobx store.

So, everything's fine with using the workaround: jest.mock('mobx-react/native', () => require('mobx-react/custom'));

However it would be nice if this workaround won't be necessary.

saimonmoore added a commit to saimonmoore/JamOn that referenced this issue Jan 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants