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

fix(jest-preset): resolve react-native starting from project root #2800

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-eels-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/jest-preset": patch
---

Resolve `react-native` starting from project root
2 changes: 2 additions & 0 deletions .changeset/twelve-peaches-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
24 changes: 13 additions & 11 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,32 @@ packageExtensions:
"@microsoft/eslint-plugin-sdl@0.x":
peerDependencies:
eslint: ">=5.16.0"
"babel-plugin-transform-flow-enums@*":
babel-plugin-transform-flow-enums@*:
peerDependencies:
"@babel/core": ^7.20.0
"memfs@4.x":
memfs@4.x:
peerDependenciesMeta:
"quill-delta":
memfs:
optional: true
"memfs":
quill-delta:
optional: true
"rxjs":
rxjs:
optional: true
"tslib":
tslib:
optional: true
"metro-transform-worker@*":
metro-transform-worker@*:
dependencies:
"metro-minify-terser": "0.76.8"
"react-native@*":
react-native-macos@*:
peerDependencies:
"@babel/preset-env": ^7.1.6
"react-native-macos@*":
react-native: "*"
react-native-windows@*:
peerDependencies:
"@babel/preset-env": ^7.1.6
"react-native": "*"
"react-native-windows@*":
react-native@*:
dependencies:
"@babel/runtime": "^7.20.0"
peerDependencies:
"@babel/preset-env": ^7.1.6
plugins:
Expand Down
44 changes: 31 additions & 13 deletions packages/jest-preset/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ function getReactNativePlatformPath(rootDir = getPackageDirectory()) {
/**
* Returns the platform name and path to the module providing the platform.
* @param {string | undefined} defaultPlatform
* @param {{ paths: string[] }} searchPaths
* @returns {PlatformPath} `[platformName, platformPath]` pair
*/
function getTargetPlatform(defaultPlatform) {
function getTargetPlatform(defaultPlatform, searchPaths) {
if (!defaultPlatform) {
// If no target platform is set, see if we're inside an out-of-tree
// platform package.
Expand Down Expand Up @@ -118,12 +119,11 @@ function getTargetPlatform(defaultPlatform) {

// `npmPackageName` is unset if target platform is in core.
const { npmPackageName } = targetPlatformConfig;
const projectRoot = { paths: [process.cwd()] };
return [
defaultPlatform,
npmPackageName
? path.dirname(
require.resolve(`${npmPackageName}/package.json`, projectRoot)
require.resolve(`${npmPackageName}/package.json`, searchPaths)
)
: undefined,
];
Expand Down Expand Up @@ -181,13 +181,15 @@ function moduleNameMapper(reactNativePlatformPath) {
* Returns setup files for React Native.
* @param {string | undefined} targetPlatform
* @param {string | undefined} reactNativePlatformPath
* @param {{ paths: string[] }} searchPaths
* @returns {string[] | undefined}
*/
function setupFiles(targetPlatform, reactNativePlatformPath) {
function setupFiles(targetPlatform, reactNativePlatformPath, searchPaths) {
return targetPlatform
? [
require.resolve(
`${reactNativePlatformPath || "react-native"}/jest/setup.js`
`${reactNativePlatformPath || "react-native"}/jest/setup.js`,
searchPaths
),
]
: undefined;
Expand All @@ -197,13 +199,19 @@ function setupFiles(targetPlatform, reactNativePlatformPath) {
* Returns test environment for React Native.
* @param {string | undefined} targetPlatform
* @param {string | undefined} reactNativePlatformPath
* @param {{ paths: string[] }} searchPaths
* @returns {string | undefined}
*/
function getTestEnvironment(targetPlatform, reactNativePlatformPath) {
function getTestEnvironment(
targetPlatform,
reactNativePlatformPath,
searchPaths
) {
if (targetPlatform) {
try {
return require.resolve(
`${reactNativePlatformPath || "react-native"}/jest/react-native-env.js`
`${reactNativePlatformPath || "react-native"}/jest/react-native-env.js`,
searchPaths
);
} catch (_) {
// ignore
Expand All @@ -216,14 +224,16 @@ function getTestEnvironment(targetPlatform, reactNativePlatformPath) {
* Returns transform rules for React Native.
* @param {string | undefined} targetPlatform
* @param {string | undefined} reactNativePlatformPath
* @param {{ paths: string[] }} searchPaths
* @returns {Record<string, string> | undefined}
*/
function transformRules(targetPlatform, reactNativePlatformPath) {
function transformRules(targetPlatform, reactNativePlatformPath, searchPaths) {
const reactNative = reactNativePlatformPath || "react-native";
return targetPlatform
? {
"\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$": require.resolve(
`${reactNative}/jest/assetFileTransformer.js`
`${reactNative}/jest/assetFileTransformer.js`,
searchPaths
),
}
: undefined;
Expand All @@ -239,21 +249,29 @@ module.exports = (
...userOptions
} = {}
) => {
const [targetPlatform, platformPath] = getTargetPlatform(defaultPlatform);
const testEnvironment = getTestEnvironment(targetPlatform, platformPath);
const searchPaths = { paths: [process.cwd()] };
const [targetPlatform, platformPath] = getTargetPlatform(
defaultPlatform,
searchPaths
);
const testEnvironment = getTestEnvironment(
targetPlatform,
platformPath,
searchPaths
);
return {
haste: haste(targetPlatform),
moduleNameMapper: {
...moduleNameMapper(platformPath),
...userModuleNameMapper,
},
setupFiles: setupFiles(targetPlatform, platformPath),
setupFiles: setupFiles(targetPlatform, platformPath, searchPaths),
testEnvironment,
transform: {
"\\.[jt]sx?$": testEnvironment
? "babel-jest"
: ["babel-jest", { presets: babelPresets(targetPlatform) }],
...transformRules(targetPlatform, platformPath),
...transformRules(targetPlatform, platformPath, searchPaths),
...userTransform,
},
transformIgnorePatterns: [
Expand Down
23 changes: 23 additions & 0 deletions packages/test-app/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("node:fs");
const path = require("node:path");

function isPnpmMode() {
const url = path.resolve(__dirname, "..", "..", ".yarnrc.yml");
const yarnConfig = fs.readFileSync(url, { encoding: "utf-8" });
return yarnConfig.includes("nodeLinker: pnpm");
}

const config = {
preset: "@rnx-kit/jest-preset/private",
moduleNameMapper: {
"^internal(.*)$": "<rootDir>/src/internal$1",
},
};

if (isPnpmMode()) {
config["transformIgnorePatterns"] = [
"/node_modules/.store/(?!((jest-)?react-native(-macos)?|@react-native(-community)?|@office-iss-react-native-win32|@?react-native-windows))",
];
}

module.exports = config;
6 changes: 0 additions & 6 deletions packages/test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@
"internal"
]
},
"jest": {
"preset": "@rnx-kit/jest-preset/private",
"moduleNameMapper": {
"^internal(.*)$": "<rootDir>/src/internal$1"
}
},
"rnx-kit": {
"kitType": "app",
"build": {
Expand Down