I have a strange problem with testing redux middleware when it comes to mocking createAsyncThunk
which really drives me crazy.
I have the following code that is included setupFiles
list in jest.config.js
import { createAction } from "@reduxjs/toolkit";
jest.doMock("@reduxjs/toolkit", () => ({
...jest.requireActual("@reduxjs/toolkit"),
createAsyncThunk: jest.fn((name) => {
const thunkAction = jest
.requireActual("@reduxjs/toolkit")
.createAsyncThunk(name, () => {});
const action = createAction(name);
(action as any).pending = thunkAction.pending;
(action as any).fulfilled = thunkAction.fulfilled;
(action as any).rejected = thunkAction.rejected;
(action as any).typePrefix = name;
return action;
}),
}));
It works well with @reduxjs/toolkit
v1.7.2, but if I upgrade to the next version (v1.8) or any subsequent versions, the middleware picks up the actual version of createAsyncThunk
rather than mocked version. I went through the difference between the versions, but did not see anything relevant at all.
I apologize if I did not provide enough info, feel free to point out and I will add it. Also, I am happy at this point to hear any ideas even if you don’t have the answer.