Skip to content

Commit

Permalink
Set BUILD_EXCLUDE_BABEL_REGISTER: true under Jest tests
Browse files Browse the repository at this point in the history
Summary:
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
  • Loading branch information
robhogan authored and facebook-github-bot committed Jan 27, 2024
1 parent fbb966b commit 43a0f07
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ const {only: _, ...nodeBabelOptions} = metroBabelRegister.config([]);
require('../scripts/build/babel-register').registerForMonorepo();
const transformer = require('@react-native/metro-babel-transformer');

// Set BUILD_EXCLUDE_BABEL_REGISTER (see ../scripts/build/babel-register.js) to
// prevent inline Babel registration in code under test, normally required when
// running from source, but not in combination with the Jest transformer.
const babelPluginPreventBabelRegister = [
require.resolve('babel-plugin-transform-define'),
{
'process.env.BUILD_EXCLUDE_BABEL_REGISTER': true,
},
];

module.exports = {
process(src /*: string */, file /*: string */) /*: {code: string, ...} */ {
if (nodeFiles.test(file)) {
Expand All @@ -46,6 +56,10 @@ module.exports = {
filename: file,
sourceType: 'script',
...nodeBabelOptions,
plugins: [
...(nodeBabelOptions.plugins ?? []),
babelPluginPreventBabelRegister,
],
ast: false,
});
}
Expand Down Expand Up @@ -78,6 +92,7 @@ module.exports = {
plugins: [
// TODO(moti): Replace with require('metro-transform-plugins').inlineRequiresPlugin when available in OSS
require('babel-preset-fbjs/plugins/inline-requires'),
babelPluginPreventBabelRegister,
],
sourceType: 'module',
});
Expand Down

0 comments on commit 43a0f07

Please sign in to comment.