Skip to content

Commit ee9bd85

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Add a custom Jest resolver to opt out from handling "exports" in tests (#51307)
Summary: Pull Request resolved: #51307 D72228547 added the `exports` field to the main `react-native` package, in which all imports from `./src/*` are explicitly disallowed. This was expected to be a breaking change and to limit the surface area of it, this diff will allow using all imports in jest tests. Changelog: [General][Added] Added a custom Jest resolver to opt out from handling "exports" in tests Reviewed By: huntie Differential Revision: D74708701 fbshipit-source-id: 9a2714f4e6f78ffbad9e56b5bb92657c9ea908ef
1 parent 4d0f2b2 commit ee9bd85

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/react-native/jest-preset.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
defaultPlatform: 'ios',
1515
platforms: ['android', 'ios', 'native'],
1616
},
17+
resolver: require.resolve('./jest/resolver.js'),
1718
transform: {
1819
'^.+\\.(js|ts|tsx)$': 'babel-jest',
1920
'^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve(
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
'use strict';
11+
12+
module.exports = (path, options) => {
13+
const originalPackageFilter = options.packageFilter;
14+
15+
return options.defaultResolver(path, {
16+
...options,
17+
packageFilter: pkg => {
18+
const filteredPkg = originalPackageFilter
19+
? originalPackageFilter(pkg)
20+
: pkg;
21+
22+
// Temporarily allow any react-native subpaths to be resolved and
23+
// mocked by Jest (backwards compatibility around RFC0894)
24+
if (filteredPkg.name === 'react-native') {
25+
delete filteredPkg.exports;
26+
}
27+
28+
return filteredPkg;
29+
},
30+
});
31+
};

0 commit comments

Comments
 (0)