I am trying to mock createAsyncThunk
from reduxjs/toolkit
as follows:
// mock.ts file
import { createAction } from "@reduxjs/toolkit";
export const mockedToolkit: unknown = {
...jest.requireActual("@reduxjs/toolkit"),
createAsyncThunk: jest.fn((name) => {
const thunkAction = jest
.requireActual("@reduxjs/toolkit")
.createAsyncThunk(name, () => {});
const normalAction = createAction(name);
// some mocking code here
// ...
return normalAction;
}),
};
// test file
import { Store } from "redux";
import { mockedToolkit } from "./mock";
jest.doMock("@reduxjs/toolkit", () => mockedToolkit);
import ... // other imports
describe //...etc.
It works well, but only if the mock call happens earlier than other imports. If I move it to the end of the imports, the test code uses a real createAsyncThunk
instead of mocked one.
I suspect it has to do with me calling a real createAction
inside the mock, but I could not figure out if it is correct and what I can do about it.