redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

Testing with Jest & Enzyme: TypeError: Cannot read property 'catch' of undefined

Open ghasemikasra39 opened this issue 5 years ago • 4 comments

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",

ghasemikasra39 avatar Sep 04 '20 12:09 ghasemikasra39

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 avatar Sep 15 '20 09:09 alexbrazier

@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

johhansantana avatar Sep 24 '21 21:09 johhansantana

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.

johhansantana avatar Sep 24 '21 21:09 johhansantana

Is this issue closed?

mljlynch avatar Dec 27 '22 15:12 mljlynch