Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial Mocking with importActual hanging the tests #3680

Closed
6 tasks done
ad1992 opened this issue Jun 27, 2023 · 2 comments
Closed
6 tasks done

Partial Mocking with importActual hanging the tests #3680

ad1992 opened this issue Jun 27, 2023 · 2 comments

Comments

@ad1992
Copy link

ad1992 commented Jun 27, 2023

Describe the bug

Hi I am trying to migrate Excalidraw tests currently using Jest to Vitest in excalidraw/excalidraw#6713 and running to issues when mocking

jest.mock("../../scene/export", () => ({
  __esmodule: true,
  ...jest.requireActual("../../scene/export"),
  exportToSvg: jest.fn(),
}));

For this equivalent vite I am using

vi.mock("../../scene/export", async (actual) => {
  const orig = await actual();
  //@ts-ignore
  return { __esmodule: true, ...orig, exportToSvg: vi.fn() };
});

It is getting mocked which I can confirm by adding console.log after the mock but as soon as I am using another module where this mocked function is being called, the tests just hang, I don't see any error so I have to again stop the tests are rerun and its the same result.
For reference this is the file where its being used - https://github.com/excalidraw/excalidraw/blob/master/src/packages/utils.ts#L1 and as you can see the first line imports are what is being mocked above

import {
  exportToCanvas as _exportToCanvas,
  exportToSvg as _exportToSvg,
} from "../scene/export";

Probably it could be related to #546 so my guess is when I am using the mocked module in some other file, it just goes in to a recursion loop and hangs the tests.

I have also tried doMock, importActual, hoisted and none of them work.
Any help would really be appreciated.
Thank you!

Reproduction

its the same issue mentioned above and reproducible in my PR excalidraw/excalidraw#6713

This is the file where I am trying to mock it https://github.com/excalidraw/excalidraw/blob/master/src/tests/packages/utils.test.ts#L6

Unfortunately the codesandbox integration with repo isn't working rn so I am not able to attach the link here, as soon as thats fixed, I can link here as well.
If there is any info which you need please let me know

System Info

#

Used Package Manager

yarn

Validations

@ad1992
Copy link
Author

ad1992 commented Jun 27, 2023

After debugging and trying out various approaches I am using spy + mockImplementation to achieve the same and thats working for now

@sheremet-va
Copy link
Member

This was fixed some time ago (cannot find PR) and should work correctly in the latest Vitest version.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants