I'm working with a Typescript Expo projectA and a libB that is npm linked from projectA.
- LibB has react-native and some other RN related libs as peerDependencies and devDependencies.
- ProjectA has the libB peer dependencies installed properly (react-native and so on)
The project works fine with a modified metro config to make it take into account the linked libB and peerDependencies, but for the Jest tests it fails with the next error:
Invariant Violation: __fbBatchedBridgeConfig is not set, cannot invoke native modulesat invariant (../libB/node_modules/invariant/invariant.js:40:15) at Object.<anonymous> (../libB/node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:166:3) at Object.<anonymous> (../libB/node_modules/react-native/Libraries/TurboModule/TurboModuleRegistry.js:13:23)
It's clear from the error that the problem is Jest is loading and trying to use the react-native version from the libB node_modules
but what I want is Jest to use the one from projectA. I tried to configure the projectA jest.config.js
(created at projectA's root dir) to make it exclude the libB node_modulues/react-native
lib but it's not working.
this is my projectA/jest.config.js:
module.exports = { preset: "jest-expo", modulePathIgnorePatterns: ["<rootDir>/node_modules/libB/node_modules/react-native", ], transformIgnorePatterns: ["<rootDir>/node_modules/libB/node_modules/((react-native|invariant)/)","<rootDir>/node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)", ], verbose: true,};
As you can see I added react-native
as exclusion in modulePathIgnorePatterns
so Jest doesn't use that react native lib because it's present directly on projectA node_modules
. I also added the same path in transformIgnorePatterns so Jest doesn't try to transform the code for that react-native dir since it's already been told to do it for the one in the projectA node_modules
.
I'm not well versed in this configs so I don't know what am I doing wrong.
Can anybody help or give me a hint about my mistake or the correct way to achieve my goal? Thank you in advance.