fix: use `require.resolve` in `jest-preset` (#22972)

Summary:
The current way the Jest preset work requires the different files to be in `<rootDir>/node_modules/react-native`. This is not necessarily true - especially in monorepoes.

If we instead do `require.resolve`, we do not need to do `rootDir` replacement in Jest.

Having a JS file as preset has been supported since Jest 23.0.0: https://github.com/facebook/jest/pull/6185

Changelog:
----------

[General] [Fixed] - use `require.resolve` in `jest-preset`
Pull Request resolved: https://github.com/facebook/react-native/pull/22972

Differential Revision: D13662758

Pulled By: hramos

fbshipit-source-id: ca79b5b89d9d05c6fe639b0d88619858e8d05da7
This commit is contained in:
Simen Bekkhus 2019-01-14 16:28:55 -08:00 committed by Facebook Github Bot
parent 1bdb250906
commit 991e83f568
3 changed files with 42 additions and 40 deletions

41
jest-preset.js Normal file
View File

@ -0,0 +1,41 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const dir = __dirname;
module.exports = {
haste: {
defaultPlatform: 'ios',
platforms: ['android', 'ios', 'native'],
hasteImplModulePath: require.resolve('./jest/hasteImpl.js'),
providesModuleNodeModules: ['react-native'],
},
moduleFileExtensions: ['js', 'json', 'jsx', 'node', 'ts', 'tsx'],
moduleNameMapper: {
'^React$': require.resolve('react'),
},
modulePathIgnorePatterns: [`${dir}/Libraries/react-native/`],
transform: {
'^.+\\.(js|ts|tsx)$': 'babel-jest',
'^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve(
'./jest/assetFileTransformer.js',
),
},
transformIgnorePatterns: [
'node_modules/(?!(jest-)?react-native|react-clone-referenced-element)',
],
testMatch: [
'**/__tests__/**/*.(js|ts|tsx)',
'**/?(*.)+(spec|test).(js|ts|tsx)',
],
setupFiles: [require.resolve('./jest/setup.js')],
testEnvironment: 'node',
};

View File

@ -1,39 +0,0 @@
{
"haste": {
"defaultPlatform": "ios",
"platforms": ["android", "ios", "native"],
"hasteImplModulePath": "<rootDir>/node_modules/react-native/jest/hasteImpl.js",
"providesModuleNodeModules": [
"react-native"
]
},
"moduleFileExtensions": [
"js",
"json",
"jsx",
"node",
"ts",
"tsx"
],
"moduleNameMapper": {
"^React$": "<rootDir>/node_modules/react"
},
"modulePathIgnorePatterns": [
"<rootDir>/node_modules/react-native/Libraries/react-native/"
],
"transform": {
"^.+\\.(js|ts|tsx)$": "babel-jest",
"^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$": "<rootDir>/node_modules/react-native/jest/assetFileTransformer.js"
},
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element)"
],
"testMatch": [
"**/__tests__/**/*.(js|ts|tsx)",
"**/?(*.)+(spec|test).(js|ts|tsx)"
],
"setupFiles": [
"<rootDir>/node_modules/react-native/jest/setup.js"
],
"testEnvironment": "node"
}

View File

@ -115,7 +115,7 @@
"scripts/launchPackager.command", "scripts/launchPackager.command",
"scripts/packager.sh", "scripts/packager.sh",
"scripts/react-native-xcode.sh", "scripts/react-native-xcode.sh",
"jest-preset.json", "jest-preset.js",
"jest", "jest",
"lib", "lib",
"rn-get-polyfills.js", "rn-get-polyfills.js",