Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2055ca0

Browse files
robhoganfacebook-github-bot
authored andcommittedJan 27, 2024
Set BUILD_EXCLUDE_BABEL_REGISTER: true under Jest tests (facebook#42690)
Summary: Pull Request resolved: facebook#42690 Issues triggered by `InspectorProxy*` tests under `packages/dev-middleware` (T169943794) can be root-caused to `dev-middleware` performing Babel registration within a test run, after Jest has hooked its own transformer. Babel registration is only required when running this code (`dev-middleware`, etc) directly from source - we already have the `BUILD_EXCLUDE_BABEL_REGISTER` mechanism to strip it out from production builds, but we currently don't prevent registration under tests, where Jest's transformer should be allowed to do its work. This adds the same `babel-plugin-transform-define` mechanism that we use for production builds to the Jest transformer. Changelog: [Internal] Prevent inadvertent Babel registration during running of repo tests Reviewed By: huntie Differential Revision: D53125777 fbshipit-source-id: 1f0a20315c96edaf79054e29a80c7a9561e5b352
1 parent 41b256a commit 2055ca0

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
 

‎jest/preprocessor.js

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ const {only: _, ...nodeBabelOptions} = metroBabelRegister.config([]);
3838
require('../scripts/build/babel-register').registerForMonorepo();
3939
const transformer = require('@react-native/metro-babel-transformer');
4040

41+
// Set BUILD_EXCLUDE_BABEL_REGISTER (see ../scripts/build/babel-register.js) to
42+
// prevent inline Babel registration in code under test, normally required when
43+
// running from source, but not in combination with the Jest transformer.
44+
const babelPluginPreventBabelRegister = [
45+
require.resolve('babel-plugin-transform-define'),
46+
{
47+
'process.env.BUILD_EXCLUDE_BABEL_REGISTER': true,
48+
},
49+
];
50+
4151
module.exports = {
4252
process(src /*: string */, file /*: string */) /*: {code: string, ...} */ {
4353
if (nodeFiles.test(file)) {
@@ -46,6 +56,10 @@ module.exports = {
4656
filename: file,
4757
sourceType: 'script',
4858
...nodeBabelOptions,
59+
plugins: [
60+
...(nodeBabelOptions.plugins ?? []),
61+
babelPluginPreventBabelRegister,
62+
],
4963
ast: false,
5064
});
5165
}
@@ -78,6 +92,7 @@ module.exports = {
7892
plugins: [
7993
// TODO(moti): Replace with require('metro-transform-plugins').inlineRequiresPlugin when available in OSS
8094
require('babel-preset-fbjs/plugins/inline-requires'),
95+
babelPluginPreventBabelRegister,
8196
],
8297
sourceType: 'module',
8398
});

0 commit comments

Comments
 (0)
Please sign in to comment.