Unit tests with jest broken after update of React Native

Viewed 477

I recently updated React Native from 0.59.10 to 0.63.1 in my project. I followed React Native Upgrade Helper to do it and the app works properly but I have some issues with my unit tests.

I used in my code Dimensions from react-native to get window's width and height so during my units tests I have always the same error

 Test suite failed to run
    TypeError: Cannot read property 'screen' of undefined

      at Function.set (node_modules/react-native/Libraries/Utilities/Dimensions.js:64:10)
      at Object.<anonymous> (node_modules/react-native/Libraries/Utilities/Dimensions.js:140:12)
      at Object.Dimensions (node_modules/react-native/index.js:334:12)```

And the code which use it in node_modules/react-native/Libraries/Utilities/Dimensions.js:

{...}
RCTDeviceEventEmitter.addListener(
    'didUpdateDimensions',
    (update: DimensionsPayload) => {
      Dimensions.set(update);
    },
  );
{...}
static set(dims: $ReadOnly<{[key: string]: any, ...}>): void {
    // We calculate the window dimensions in JS so that we don't encounter loss of
    // precision in transferring the dimensions (which could be non-integers) over
    // the bridge.
    let {screen, window} = dims;  **<=== IT FAILS HERE because dims is undefined**
{...}

I never saw this error before the react native's update.

I tried to mock Dimensions and RCTDeviceEventEmitter in my jest setup file like that

jest.mock('../node_modules/react-native/Libraries/Utilities/Dimensions');

jest.mock('../node_modules/react-native/Libraries/EventEmitter/RCTDeviceEventEmitter', () => {
  return {
    ...jest.requireActual(
      '../node_modules/react-native/Libraries/EventEmitter/RCTDeviceEventEmitter'
    ),
    addListener: jest.fn().mockResolvedValue(() => {
      return {
        update: jest.fn(),
      };
    }),
  };
});

I tried to mock them with other data but still the same issue. If someone has an idea, welcome :)

Thanks for answers.

"react": "16.13.1",

"react-native": "0.63.1",

"jest": "24.9.0",

0 Answers
Related