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

This commit is contained in:
Tommy Nguyen 2023-11-07 18:42:19 +01:00 коммит произвёл GitHub
Родитель 15362b7a9a
Коммит dafd4e9522
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 74 добавлений и 30 удалений

Просмотреть файл

@ -0,0 +1,5 @@
---
"@rnx-kit/jest-preset": patch
---
Resolve `react-native` starting from project root

Просмотреть файл

@ -0,0 +1,2 @@
---
---

Просмотреть файл

@ -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:

Просмотреть файл

@ -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.
@ -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,
];
@ -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;
@ -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
@ -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;
@ -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: [

Просмотреть файл

@ -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;

Просмотреть файл

@ -67,12 +67,6 @@
"internal"
]
},
"jest": {
"preset": "@rnx-kit/jest-preset/private",
"moduleNameMapper": {
"^internal(.*)$": "<rootDir>/src/internal$1"
}
},
"rnx-kit": {
"kitType": "app",
"build": {