-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Clear and concise description of the problem
Traditional file imports like import { foo } from '../bar' or const foo = await import('../bar') have the advantage, that when the file bar is being renamed or moved to a different folder, TypeScript automatically renames the path on all import occurrences.
Mocking with vi.mock('../bar') doesn't get the same renaming support. When you rename/move the file bar, the import statements are updated but the mocks are not. That means suddenly, the tests fail and it may not be obvious why.
Here is a short video in case it's not clear:
2024-05-05_13-22-29.mp4
Another disadvantage, although less important, is the navigation feature that editors provide for import statements. On macOS with CMD+mouse click you can easily navigate to the source file.
Suggested solution
If it were possible to provide a dynamic import to mock like vi.mock(import("./bar")) so we would get the auto-renaming from TypeScript. Then, the Vitest could apply a similar logic to vi.mock() like it does for vi.hoisted(). It could rewrite all mocks to replace the vi.mock(import("./bar")) with the old vi.mock("./bar"). Maybe it is also possible to work with the Promise from import() directly inside vi.mock and it is not even necessary to rewrite all mock statements.
Alternative
No response
Additional context
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status