redux-persist
redux-persist copied to clipboard
Testing with Jest & Enzyme: TypeError: Cannot read property 'catch' of undefined
The package works perfectly fine in a non-testing env. However, in a testing environment, I am getting this error:
TypeError: Cannot read property 'catch' of undefined
at writeStagedState (/Users/kasra/Documents/app/node_modules/redux-persist/lib/createPersistoid.js:98:71)
at Timeout.processNextKey [as _onTimeout] (/Users/kasra/Documents/app/node_modules/redux-persist/lib/createPersistoid.js:87:7)
"react-native": "0.62.2",
"redux-persist": "^6.0.0"
"redux": "^4.0.4"
"react-redux": "^7.1.3"
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.4",
"jest": "^26.4.2",
"jest-enzyme": "^7.1.2",
"jest-expo": "^38.0.2",
I solved it using this mock:
jest.mock('redux-persist', () => {
const real = jest.requireActual('redux-persist');
return {
...real,
persistReducer: jest
.fn()
.mockImplementation((config, reducers) => reducers),
};
});
It basically just bypasses redux-persist by returning the reducers directly without wrapping them in redux-persist.
You can also downgrade to jest: 25.x.x.
@alexbrazier I tried your solution but I still get the error message, though the tests pass for some reason. I am using "jest": "^26.6.3" though, not sure if this has to do.
writePromise = storage.setItem(storageKey, serialize(stagedState)).catch(onWriteFail);
^
TypeError: Cannot read property 'catch' of undefined
ok, so this seems to be an error with the API of AsyncStorage. Following https://github.com/react-native-async-storage/async-storage/issues/379#issuecomment-660478051 answer I added the mock and it works now.
Is this issue closed?