I recently started using Immer in redux reducers in my react app since I have a lot of nested states in them. (lets avoid the fact the these nesting can be solved with sub reducers).
The usage of Immer is clear for me, but once I started to write unit tests with jest I started wondering, that should I avoid using Immer in the tests?
Lets have a basic reducer example:
export default function (state = initialState, action) {
return produce(state, (draftState) => {
switch (action.type) {
case MY_TYPE:
draftState.some.nested.flag = true;
break;
}
});
}
then my test which also using Immer
it('should handle MY_TYPE', () => {
const storeState = reducer(initialState, {
type: MY_TYPE
});
const newState = produce(initialState, (draftState) => {
draftState.some.nested.flag = true;
});
expect(storeState).toEqual(newState);
});
So my question is that should I avoid using Immer produce in the tests and make the copy of the nested object manually with the spread syntax ? Something like:
.toEqual({
...initialState,
some: {
...initialState.some,
nested: {
...initialState.some.nested,
flag: true
}
}
})
So is there any pitfalls using Immer in tests ?