Skip to content

[tests] UIManager.js mutates NativeModules, failing after jest.resetModules() #24401

Closed
@ide

Description

@ide

🐛 Bug Report

UIManager.js mutates NativeModules, which is mocked by setup.js. The mutation is done in a way that is not idempotent, causing fields of the UIManager to be undefined on the second run.

In an application, UIManager.js is run just once because all modules are singletons in JS. The exception is when we take over the module system with something like Jest. Calling jest.resetModules() clears the module cache, which includes UIManager.js but not setup.js. As a result, the second time UIManager.js is run, it gets an already mutated version of NativeModules.

To Reproduce

Write a Jest test with two specs loading the UIManager and jest.resetModules() in an afterEach block.

Expected Behavior

jest.resetModules() should not cause RN tests to fail.

Code Example

afterEach(() => {
  jest.resetModules();
});

it(`a`, () => {
  require('react-native').findNodeHandle;
});

it(`b`, () => {
  require('react-native').findNodeHandle;
});

Environment

This is with RN 0.59.3.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions