-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(browser): fix mocking from outside of root #7789
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
Merged
sheremet-va
merged 6 commits into
vitest-dev:main
from
hi-ogawa:fix-browser-mock-importer-out-of-root
Apr 17, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fde59e5
fix(browser): fix mocking from outside of root
hi-ogawa 2484bda
fix: fix public mocker
hi-ogawa eeded51
test: add test
hi-ogawa aea6ea1
test: tweak
hi-ogawa 5a435ca
test: tweak
hi-ogawa c1ebb34
refactor: getModuleById
hi-ogawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/browser/fixtures/mocking-out-of-root/project1/basic.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { vi, test, expect } from "vitest" | ||
| import lib from "./lib.js"; | ||
|
|
||
| vi.mock("./lib.js", () => ({ default: "mocked" })) | ||
|
|
||
| test("project3", () => { | ||
| expect(lib).toBe("mocked"); | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default "lib" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the current logic is that if it doesn't start with root, then it's
/relative-from-root.js, so we can safely just join them. TheresolveIdhere seems over complicated, importer should always be resolved/partially resolvedI assume
importerhere has/@fs/or something (this is what vite does to files outside of root), so we can just strip itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wasn't the case since normal usages (other than public-mocker test) already has fully resolved absolute path (i.e. resolved id), for example on
test/browser/fixtures/mocking-out-of-roottest case:This wasn't about
/@fs, which is already stripped out duringgetImporterand stack trace parsing:vitest/packages/utils/src/source-map.ts
Lines 69 to 72 in eeded51
Non fully resolved id only happend for public-mocker test which doesn't have the same logic. Alternative is to add extra mocker option to applying joining only for public-mocker usage.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I just realized project root path is not only public-mocker test, for example
test/browser/fixtures/mocking-out-of-root/project1/imported-test.js:We'll need to somehow tell mocker about whether
(abs-path)/project3/index.jswas originally with/@fsor not, so we can skip the joining.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think this extra
resolveIdis also fine. If you know other idea, I would be happy to hear.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I still think
resolveIdis overkill because we know exactly how this value is resolved already. Maybe we can just checkmoduleGraph.getByUrland return themodule.fileif it exists, with a fallback toimporterwithout joining