Jest Tests broken with SDK 18 (RN 0.45)

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.

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

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: jest-expo window.WebSocket not defined · Issue #304 · expo/expo · GitHub

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)

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 jest-expo window.WebSocket not defined · Issue #304 · expo/expo · GitHub to see updates on that

1 Like

glad that fixed the snapshotting issue!