vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Mocking cyclic dependencies with `importOriginal` doesn't work in the browser

Open sheremet-va opened this issue 1 year ago • 2 comments

Describe the bug

When mocking the cyclic dependency with an importOriginal/vi.importActual inside, the transformation hangs.

Reproduction

The same as:

https://github.com/vitest-dev/vitest/blob/839c39f06700144694677f94f8561f4d19a60b51/test/core/test/mocking/cyclic-import-actual.spec.ts#L5

System Info

Any

Used Package Manager

pnpm

Validations

sheremet-va avatar Jun 03 '24 10:06 sheremet-va

Am I correct in understanding that the importOriginal module request is still mocked when it shouldn't be?

https://github.com/vitest-dev/vitest/blob/839c39f06700144694677f94f8561f4d19a60b51/test/core/test/mocking/cyclic-import-actual.spec.ts#L6

Or is the root cause elsewhere entirely?

kettanaito avatar Oct 15 '24 21:10 kettanaito

Yes, it should return the actual module. In vite-node, we can track the call stack, but I am not sure yet if it's possible to make it work in the browser. Maybe we can inject some kind of a query to ignore in the mock handler.

sheremet-va avatar Oct 16 '24 11:10 sheremet-va

Any updates on this? It would be useful to let us see an error message instead of hanging the browser.

CaoKha avatar Jul 18 '25 08:07 CaoKha

Any updates on this? It would be useful to let us see an error message instead of hanging the browser.

Nothing new. Throwing an error would be nice, but I am not sure how to track it yet.

sheremet-va avatar Jul 22 '25 13:07 sheremet-va

I'm in the process of migrating from jest to vitest and this has caused me so much headache 🤕 Glad I found this issue to at least know what is happening.

Do you think this is worthy of a FYI in the docs?

todor-a avatar Oct 10 '25 10:10 todor-a

I'm in the process of migrating from jest to vitest and this has caused me so much headache 🤕 Glad I found this issue to at least know what is happening.

Do you think this is worthy of a FYI in the docs?

Yes, we can at least add it to the mocking guide. I think there are already some limitations listed.

sheremet-va avatar Oct 12 '25 11:10 sheremet-va