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

Mocking breaks when isolate: false, singleThread: true, v1.1.2 #4894

Closed
6 tasks done
AriPerkkio opened this issue Jan 7, 2024 · 12 comments
Closed
6 tasks done

Mocking breaks when isolate: false, singleThread: true, v1.1.2 #4894

AriPerkkio opened this issue Jan 7, 2024 · 12 comments

Comments

@AriPerkkio
Copy link
Member

AriPerkkio commented Jan 7, 2024

Note: This issue is specifically about isolate: false, singleThread: true configuration.

Describe the bug

Likely regression from #4664, cc. @Dunqing

If two files are loading same file and the second one mocks that file, Vitest will throw Error: [vitest] Cannot mock "../src/basic.js" because it is already loaded. Did you import it in a setup file? error. Reproduction below has no setup files - just two test files importing same source file while second one mocks that file.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-scpuyh?file=test%2Fsecond.test.ts,test%2Ffirst.test.ts

System Info

Stackblitz

Used Package Manager

pnpm

Validations

@AriPerkkio
Copy link
Member Author

Coverage tests are failing randomly, depending which file Vitest's cache decides to run first:

import { expect, test, vi } from 'vitest'
import { add } from '../src/utils'
vi.mock('../src/utils', async () => ({
add: vi.fn().mockReturnValue('mocked'),
}))
test('mocking in Javascript test should not break sourcemaps', () => {
expect(add(1, 2)).toBe('mocked')
})

@sheremet-va
Copy link
Member

sheremet-va commented Jan 7, 2024

Not sure if it is a bug. If the file structure was a little bit different (file was imported inside another file), there would've been a situation when mocking wouldn't have worked in Vitest 1.0 (before the error that catches such bugs/features was introduced):

https://stackblitz.com/edit/vitest-dev-vitest-gxiruo?file=src%2Fbasic2.ts,package.json,test%2Ffirst.test.ts,test%2Fsecond.test.ts

The error was introduced in #4862

@roman-gula
Copy link

Same issue, is reproduced also for 1.1.3

@AriPerkkio
Copy link
Member Author

Right, if this is expected behaviour then maybe the error message should be different when it's caused by disabled isolation. Seeing error message that mentions setupFiles is a bit misleading in cases like the minimal repro.

@AriPerkkio AriPerkkio added discussion and removed bug labels Jan 8, 2024
@sheremet-va
Copy link
Member

Right, if this is expected behaviour then maybe the error message should be different when it's caused by disabled isolation. Seeing error message that mentions setupFiles is a bit misleading in cases like the minimal repro.

It is not possible to know why the error happens.

@AriPerkkio
Copy link
Member Author

AriPerkkio commented Jan 8, 2024

The possible causes could still be narrowed down based on configurations. If there are no setupFiles, do not mention that. If isolation is disabled, maybe add that one there.

@sheremet-va
Copy link
Member

This should be fixed by #4905

This error is now thrown only for isolated environments.

@Tohid001
Copy link

@sheremet-va hey mate, I have written hundreds of test cases mocking react's particular api's that's needed mocking. And now all those tests are getting failed with this error thrown:
Please, remove the import if you want static imports to be mocked, or clear module cache by calling "vi.resetModules()" before mocking if you are going to import the file again. See: https://vitest.dev/guide/common-errors.html#cannot-mock-mocked-file-js-because-it-is-already-loaded

I have tried the resetmodules solution but it did not work for me

@divmgl
Copy link

divmgl commented Jan 23, 2024

We're having the same problem. In the v0 version of Vitest things work as expected (we're on v0.34.6). The latest version of Vitest fails with this same error.

@sheremet-va
Copy link
Member

sheremet-va commented Jan 23, 2024

Please, provide reproductions when filing an issue. Any comment that basically means "+1" will be ignored.

@i-void
Copy link

i-void commented Jan 23, 2024

I have the same issue. Reproducing is easy. In my case creating a test file with a mock for top level imported module and disable isolation (singleFork or isolation: false) is enough to see the error. For just maxFork: 1 minFork. 1 it works but this time tests are 10x slower.

@divmgl
Copy link

divmgl commented Jan 23, 2024

Please, provide reproductions when filing an issue. Any comment that basically means "+1" will be ignored.

I went into StackBlitz and attempted to recreate the issue to no avail. I spent some time in our codebase commenting out code until I narrowed down where it was happening. We have a complex web of dependencies and I noticed one function that was exported in a module that had a ton of different imports. I moved that function into a different module and the issue stopped happening.

If I had to guess, this issue occurs when there's a circular dependency between two modules that indirectly import the mocked module. Hopefully this helps as I won't be able to spend any more time on this. cc @sheremet-va

@github-actions github-actions bot locked and limited conversation to collaborators Feb 7, 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

6 participants