Skip to content

Doesn't work jest.mock function #34

@vqc1909a

Description

@vqc1909a

Hi everyone, one question, I'm using jest": "^29.7.0", "jest-fixed-jsdom": "^0.0.9" and "msw": "^2.10.2" into a project, and basically doesn't mock my functions as the following

const dispatch = jest.fn();

jest.mock("../../../store/hooks", () => ({
	...jest.requireActual("../../../store/hooks"),
	useAppDispatch: () => dispatch,
}));
jest.mock("../../../store/slices/ecommerce/thunks", () => ({
	...jest.requireActual("../../../store/slices/ecommerce/thunks"),
	startAddToCart: jest.fn(),
}));

So, i get an error because dispatch is not called

Image

But, if i use jest.spyOn instead of the jest.mock, it works perfectly as the following

import * as hooks from "../../../store/hooks";
import * as thunks from "../../../store/slices/ecommerce/thunks";

const dispatch = jest.fn();

jest.spyOn(hooks, "useAppDispatch").mockImplementation(() => dispatch);
jest.spyOn(thunks, "startAddToCart").mockImplementation(jest.fn());

Image

I don't know if it's an mistake by jest-fixed-jsdom or msw, because i have another project with the same version of jest but without msw neither jest-fixed-jsdom, and this work perfectly using jest.mock

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions