Skip to content

Create a custom Jest environment #509

@SimenB

Description

@SimenB

Introduction

The React Native Jest preset currently sets up jest-environment-node as a test environment, see https://github.com/facebook/react-native/blob/4e70376dc722b6beb7b5b6d6c042b748a9ed14fd/jest-preset.js#L27. This environment does not properly emulate an RN environment, and leads to unnecessary incompatibilities between tests and actual production code - meaning less reliable tests.

Details

I would like to see React Native ship their own environment instead of relying on one of the two shipped by Jest.

By not doing do, a bunch of Node-specific APIs are available in tests which are not available at runtime (e.g. Buffer, MessageChannel, a native fetch instead of your polyfill etc.).

Another issue is the exports support introduced in Jest 28 - this means the Node environment will resolve the node and node-addon condition instead of e.g. falling back to the main field, leading to different modules being loaded in test and in "real" code. And since Metro will be supporting it, this means the test env won't resolve modules correctly once modules start using exports for RN modules.

The API is defined here: https://github.com/facebook/jest/blob/74d27a746d6279141871f761efd1d7859e49e2dc/packages/jest-environment/src/index.ts#L40-L51

If this proposal is accepted, I'm happy to help review (or write) code needed for this to happen.

Discussion points

React Native defaults to a test environment which behaves more different than needed from a "real" environment. It would be better if RN shipped its own environment to more accurately match the environment of production code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions