I'm unable to mock platform selection due to jest being unable to find the module
Error Message 1
tests/__tests__/base.test.js
● Test suite failed to run
Cannot find module './Libraries/Utilities/Platform' from 'node_modules/react-native/index.js'
Require stack:
src/theme/index.js
src/tools/index.js
src/db/models/subscription.js
src/db/realm.js
tests/realm.js
tests/__tests__/base.test.js
However, Jest was able to find:
'Libraries/Utilities/Platform.android.js'
'Libraries/Utilities/Platform.d.ts'
'Libraries/Utilities/Platform.ios.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'].
When removing the ...ReactNative.Platform line from jest.setup.js the following error occours:
Error Message 2
Invariant Violation: __fbBatchedBridgeConfig is not set, cannot invoke native modules
13 | },
14 | NativeModules: {
> 15 | ...ReactNative.NativeModules,
I have tried adding '^Libraries/Utilities/Platform$': '<rootDir>/node_modules/react-native/Libraries/Utilities/Platform.android.js', to the moduleNameMapper
jest.config.js
module.exports = {
preset: 'ts-jest',
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
},
moduleDirectories: ['node_modules', 'src'],
transformIgnorePatterns: ['/node_modules/(?!react-native)/.+'],
setupFiles: ['./tests/jest.setup.js'],
globals: {
__DEV__: true,
},
moduleNameMapper: {
'^react-native$': '<rootDir>/node_modules/react-native',
},
}
jest.setup.js
import * as ReactNative from 'react-native'
jest.doMock('react-native', () => {
return Object.setPrototypeOf(
{
Dimensions: {
get: () => ({ width: 414, height: 896 }),
},
Platform: {
...ReactNative.Platform,
OS: 'android',
select: jest.fn((selector) => selector),
},
NativeModules: {
...ReactNative.NativeModules,
SettingsManager: {
settings: {
AppleLocale: 'en_US',
},
},
I18nManager: {
localeIdentifier: 'en_US',
},
},
},
ReactNative
)
})
Do I need a module resolver plugin?