Skip to content

Commit

Permalink
fix(babel): ensure components compiled with react-compiler are tested…
Browse files Browse the repository at this point in the history
… in Jest

- Fixed an issue where components compiled using `react-compiler` were not being tested in Jest.
- Updated ReactCompilerConfig to exclude test files from compilation but still compile components imported in tests.
  • Loading branch information
piotrski committed Oct 22, 2024
1 parent 0f5b0f1 commit 215f496
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const ReactCompilerConfig = {
environment: {
enableTreatRefLikeIdentifiersAsRefs: true,
},
// We exclude 'tests' directory from compilation, but still compile components imported in test files.
sources: (filename) => !filename.includes('tests/'),
};
/**
* Setting targets to node 20 to reduce JS bundle size
Expand Down Expand Up @@ -52,6 +54,7 @@ const webpack = {
const metro = {
presets: [require('@react-native/babel-preset')],
plugins: [
['babel-plugin-react-compiler', ReactCompilerConfig], // must run first!
// This is needed due to a react-native bug: https://github.com/facebook/react-native/issues/29084#issuecomment-1030732709
// It is included in metro-react-native-babel-preset but needs to be before plugin-proposal-class-properties or FlatList will break
'@babel/plugin-transform-flow-strip-types',
Expand Down Expand Up @@ -154,11 +157,5 @@ module.exports = (api) => {
const runningIn = api.caller((args = {}) => args.name);
console.debug(' - running in: ', runningIn);

// don't include react-compiler in jest, because otherwise tests will fail
if (runningIn !== 'babel-jest') {
// must run first!
metro.plugins.unshift(['babel-plugin-react-compiler', ReactCompilerConfig]);
}

return ['metro', 'babel-jest'].includes(runningIn) ? metro : webpack;
};

0 comments on commit 215f496

Please sign in to comment.