Skip to content

Include a custom resolver in Jest preset #512

@SimenB

Description

@SimenB

Introduction

Metro resolves a custom field in package.json called react-native. This is not supported by Jest's default resolver - just like e.g. module isn't. This means different modules are resolved when running tests than when metro is bundling code.

Details

The Jest preset in react-native could ship a resolver which uses e.g. packageFilter to prioritize react-native over main etc. (there's an example for modules in the docs, but I guess react-native is more like browser and needs more logic - using metro-resolver somehow probably makes the most sense).

While the long term solution to this issue is probably exports and passing a react-native condition, Metro (currently) doesn't support exports and regardless, that'll leave all existent modules in the dust.

Note that this field seems to be undocumented? Sorta mentioned here, but not really 😅

Like with #509, I'd be happy to help review and/or write the code if this is accepted.

Discussion points

Different resolution of module leads to more different code than necessary running in tests vs production code, making them more brittle and less useful. Providing a custom resolver in the preset could help alleviate this.

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