Redux warning only appearing in tests
After a lot of banging my head against the keyboard, I've found some clues.
Quick fix: make a normal export
for gameStateReducer
instead of a default
and import it elsewhere with import { gameStateReducer } from // ...
.
Jest does automatic mocking of some imports, and apparently it's buggy in this case, or it expects some sort of configuration that react-create-app
hasn't provided.
As to why Jest is even logging that error (considering the relevant code is not imported in any of the tests), I suppose it's due to its inner workings.
Supporting this is the following evidence: adding a --testPathPattern
flag to the npm test
script matching only files ending in .test.js
does not change the behaviour and the error still appears.
How to investigate the problem: I started by figuring out whether the code causing the error log was in the tests or not (indirectly it could have been, with an import
for example). After ruling this out, I started mocking the functions passed to combineReducers
to make sure any changes affected the outcome of the error log, and they did. Simply adding this:
gameStatus: gameStatusReducer || (() => ())
...already solves the issue, which means what's wrong is the import
and how Jest/babel-jest
handles it.
If I were to investigate this issue further, I would create my own installation with Jest and provide my own configuration, to see if Jest still compiles the entire application. It seems non-trivial to pass configuration options to Jest if you use react-create-app
.
Good luck!
Also had this error, but using TypeScript.
For the sake of argument, let's say our error was No reducer provided for key "favourites"
.
For the test causing this error, we imported an interface from that reducer's file, which in turn obviously runs the file to generate the exports.
Our favourites
reducer imports our Redux store to query another bit of the state as part of one of its switch cases.
For some reason, commenting out the line where we access Store.getState().someOtherReducersState
made the error go away.
So the fix was to simply jest.mock('../route/to/Store');
in the test file causing the error output.