My jest tests run/pass until I start to import modules from my local project. I'm sure there is some path/setting I'm missing...
- React native builds and runs without a problem just running tests is failing
- I have looked at many other issues w/many different search terms...
- I am NOT trying to mock anything ATM.
- I'm using jest.config.js, tsconfig.test.json and babel; all included below
Here is the error
FAIL src/__tests__/reducers/sessionSlice.test.ts● Test suite failed to run TypeError: Cannot read property 'type' of undefined 287 | return state; 288 | });> 289 | builder.addCase(EPILOG_FLASH_INDICATION_REQUEST, (state: SessionState, action: any) => { | ^ 290 | const device = getDeviceByPeripheralSelector(state, action.peripheral); 291 | device.indicationState = action.value; 292 | setDeviceDisplayStatus(device); at Object.addCase (node_modules/@reduxjs/toolkit/src/mapBuilders.ts:95:33) at builderCallback (src/reducers/sessionSlice.ts:289:17) at executeReducerBuilderCallback (node_modules/@reduxjs/toolkit/src/mapBuilders.ts:128:3) at createSlice (node_modules/@reduxjs/toolkit/src/createSlice.ts:227:9) at Object.<anonymous> (src/reducers/sessionSlice.ts:156:29) at Object.<anonymous> (src/reducers/index.ts:5:1)
Like I mentioned, react native works with these constants imported but they do not from here while running jest. So the error message is from redux but means that EPILOG_FLASH_INDICATION_REQUEST is undefined or null. So that values is not being found and imported by jest for this reducer.Here is the import from the reducer:
import { BLE_DEVICE_DISCONNECTED, EPILOG_FLASH_INDICATION_REQUEST, EPILOG_FLASH_INDICATION_REQUEST_COMPLETE, EPILOG_READY, BLE_DEVICE_FOUND,} from 'actions';
the EPILOG_* values above are imported from src/lib/epilogX6/actions.ts
folder structure././__tests__/reducers/sessionSlice.test.ts // this is the test I'm running../android./src./src/__tests__./src/lib/epilogX6/actions.ts./src/lib/epilogX6/reducers/epilogStateReducer.ts./src/actions/index.ts // this exports * form all projects in lib and other items in this file./src/reducers ./src/reducers/sessionSlice.ts // file in here is what target of test and imports {named things} from 'actions'
Configurationsjest.config.js
module.exports = { globals: { __DEV__: true, NODE_ENV: 'test','ts-jest': { tsConfig: 'tsconfig.test.json', diagnostics: { warnOnly: true, }, }, }, preset: 'react-native', moduleFileExtensions: ['ts', 'tsx', 'js'], // moduleNameMapper: { // actions: '<rootDir>/src/actions', // this worked once but didn't work for reducers found in ./src/lib // }, moduleDirectories: ['node_modules', 'src'], transformIgnorePatterns: ['node_modules/(?!(jest-)?react-native|@?react-navigation)'], transform: {'^.+\\.(js|ts)$': '<rootDir>/node_modules/babel-jest', }, testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$', testPathIgnorePatterns: ['\\.snap$', '<rootDir>/node_modules/'], cacheDirectory: '.jest/cache', // coverageDirectory: 'coverage', collectCoverageFrom: ['src/**/*.{ts,tsx,js,jsx}', '!src/**/*.d.ts'],};
tsconfig.test.json
{"extends": "./tsconfig.json","compilerOptions": {"isolatedModules": false }}
tsconfig.json
{"extends": "./tsconfig.base.json","compilerOptions": {"baseUrl": ".","paths": { // when changed here also change in babel.config.js"*": ["node_modules/*", "*"],"actions": ["./src/actions/index"],"components/*": ["./src/components/*"],"constants/*": ["./src/constants/*"],"images/*": ["./src/images/*"],"lib/*": ["./src/lib/*"],"reducers/*": ["./src/reducers/*"],"sagas/*": ["./src/sagas/*"],"screens/*": ["./src/screens/*"],"utils/*": ["./src/utils/index"],"types": ["./src/types/index"] },"plugins": [{"transform": "ts-optchain/transform"}] },"include": ["./src", "src/types"],"exclude": ["./src/screens/*","./tmp/*","**/__tests__/*","**/*/*.test.*","**/__mocks__/*","node_modules","babel.config.js","metro.config.js","jest.config.js" ]}
tsconfig.base.json
{"compilerOptions": {"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,"module": "es2015" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,"lib": ["es6", "es2018.promise"] /* Specify library files to be included in the compilation. */,"allowJs": true /* Allow javascript files to be compiled. */,"checkJs": false /* Report errors in .js files. */,"jsx": "react" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,"declaration": true /* Generates corresponding '.d.ts' file. */,"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,"sourceMap": true /* Generates corresponding '.map' file. */,"noEmit": true /* Do not emit outputs. */,"isolatedModules": true /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */,"strict": false /* Enable all strict type-checking options. */,"noUnusedLocals": true /* Report errors on unused locals. */,"noUnusedParameters": true /* Report errors on unused parameters. */,"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ }}
and just incase this is relevantbabel.config.js
module.exports = (api) => { api.cache.using(() => process.env.NODE_ENV); const presets = ['module:metro-react-native-babel-preset']; const plugins = [ ['dynamic-import-node'], ['module-resolver', { root: ['./src'], extensions: ['.js', '.ts', '.tsx', '.ios.js', '.android.js'], alias: { // when changed here also change in tsconfig.json actions: './src/actions', components: './src/components', constants: './src/constants', images: './src/images', lib: './src/lib', reducers: './src/reducers', sagas: './src/sagas', screens: './src/screens', utils: './src/utils', types: './src/types', }, }, ], ]; return { presets, plugins, };};