Skip to content

Commit

Permalink
Set BUILD_EXCLUDE_BABEL_REGISTER: true under Jest tests (#42690)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
robhogan authored and facebook-github-bot committed Jan 27, 2024
1 parent 41b256a commit 2055ca0
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 2055ca0

Please sign in to comment.