Jest Tests broken with SDK 18 (RN 0.45)

#1

It looks like react-native 0.45 broke some test related things.

First, snapshot tests are broken

Code:

import React from 'react';
import 'react-native';
import renderer from 'react-test-renderer';

import App from '../main';

test('renders without crashing', () => {
  const rendered = renderer.create(<App />).toJSON();
  expect(rendered).toMatchSnapshot();
});

Results in:

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

    Cannot find module 'react/lib/ReactComponentTreeHook' from 'ReactDebugTool.js'

      at Resolver.resolveModule (node_modules/jest-cli/node_modules/jest-resolve/build/index.js:179:17)
      at Object.<anonymous> (node_modules/react-test-renderer/lib/ReactDebugTool.js:16:30)

Second, window object no longer exists in test. I see that in node_modules/jest-expo/setup.js, a recent update added this:

// window isn't defined as of react-native 0.45+ it seems
if (typeof window !== 'object') {
  global.window = global;
  global.window.navigator = global.window.navigator || {};
}

However, this is not sufficient for apps that depend on other utilities in the window object such as window.WebSocket.

#2

Can you paste your package.json here? I think you might have an old version of react-test-renderer maybe?

#3

hi there! sorry about this - it seems there were more changes than I had thought with jest support in react-native 0.45, I created an issue to track this: https://github.com/expo/expo/issues/304

if you have time to investigate and submit a PR that would be very welcome! http://github.com/expo/jest-expo

also, as charlie pointed out, make sure react-test-renderer is up to date – it should be 16.0.0-alpha.12 (same as react version)

#4

I updated react-test-renderer from 15.6.1 to 16.0.0-alpha.12 and that fixed the snapshotting issue. Thanks!

I will follow https://github.com/expo/expo/issues/304 to see updates on that

1 Like
#5

glad that fixed the snapshotting issue!