Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-expo]: wrong TypeScript mappings using Jest Projects #28559

Open
ernestostifano opened this issue May 1, 2024 · 1 comment
Open

[jest-expo]: wrong TypeScript mappings using Jest Projects #28559

ernestostifano opened this issue May 1, 2024 · 1 comment
Assignees
Labels
CLI Versioned Expo CLI -- `npx expo start` needs more info To be used when awaiting reporter response

Comments

@ernestostifano
Copy link

ernestostifano commented May 1, 2024

Summary

Hello,

I am using jest-expo v50.0.4 along with Jest Projects and it is creating a wrong entry in the moduleNameMapper field.

Particularly, given the following simplified repo structure:

.
├── packages
│   └── the-jest-project
│       └── jest.config.cjs
└── tsconfig.json

And the following paths in the tsconfig.json file:

"paths": {
    "@my-scope/*": ["./packages/*"]
}

Running jest --show-config outputs the following for the project's moduleNameMapper field:

"moduleNameMapper": [
  [
    "[.]module[.]css$",
    "identity-obj-proxy"
  ],
  [
    "^([.]{1,2}/.*)[.]js$",
    "$1"
  ],
  [
    "^@my-scope/(.*)$",
    "/path/to/repository/packages/the-jest-project/packages/$1"
  ],
  [
    "^react-native-vector-icons$",
    "@expo/vector-icons"
  ],
  [
    "^react-native-vector-icons/(.*)",
    "@expo/vector-icons/$1"
  ]
]

The mapped value "/path/to/repository/packages/the-jest-project/packages/$1" is obviously wrong. And it breaks all matching tests.

This happens because you are using <rootDir> here, which within Jest Projects refers to the project (package) root and not the root of the repository. Instead, TypeScript mappings refer to the root of the repository.

This potentially will break many projects, especially monorepos, which are encouraged to implement Jest Projects approach.

I would suggest to make your TypeScript mappings feature opt-in (or allow to opt-out to no introduce breaking changes) or drop it altogether since I am not sure you will be able to consistently adapt it to the multiple possible edge cases.

Additionally, your moduleNameMapper values cannot be overridden. My temporary solution was to add the following entries before, with a pattern that does not break:

"moduleNameMapper": [
  [
    "[.]module[.]css$",
    "identity-obj-proxy"
  ],
  [
    "^([.]{1,2}/.*)[.]js$",
    "$1"
  ],
  [
    "^@my-scope/core/jest-setup",
    "/path/to/repository/packages/core/src/jest.setup"
  ],
  [
    "^@my-scope/([A-z0-9-]+)/dev$",
    "/path/to/repository/packages/$1/src/dev"
  ],
  [
    "^@my-scope/([A-z0-9-]+)$",
    "/path/to/repository/packages/$1/src"
  ],
  [
    "^@my-scope/(.*)$",
    "/path/to/repository/packages/the-jest-project/packages/$1"
  ],
  [
    "^react-native-vector-icons$",
    "@expo/vector-icons"
  ],
  [
    "^react-native-vector-icons/(.*)",
    "@expo/vector-icons/$1"
  ]
]

Now, this is not ideal for many reasons, but especially because it breaks native ESM exports resolution, for example:

testEnvironmentOptions: {
    customExportConditions: [
        'development',
        'react-native',
        'require',
        'default'
    ]
}

Because the exports field in a package JSON could have many complex configurations that are not replicable using moduleNameMapper alone.

What platform(s) does this occur on?

No response

SDK Version

N/A

Environment

N/A

Minimal reproducible example

N/A

@ernestostifano ernestostifano added CLI Versioned Expo CLI -- `npx expo start` needs validation Issue needs to be validated labels May 1, 2024
@expo-bot expo-bot removed the needs validation Issue needs to be validated label May 1, 2024
@byCedric byCedric self-assigned this May 5, 2024
@byCedric
Copy link
Member

Hi @ernestostifano! Thanks for the issue report. I'm not sure if I follow the results from the moduleNameMapper here. It should indeed use <rootDir>, but not the absolute path that's you mentioned.

This is modeled after ts-jest's own tsconfig path mapper to jest (here, snapshot test).

Is it possible to provide a repro for us to investigate the issue further?

@byCedric byCedric added the needs more info To be used when awaiting reporter response label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI Versioned Expo CLI -- `npx expo start` needs more info To be used when awaiting reporter response
Projects
None yet
Development

No branches or pull requests

3 participants