-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix(browser): fix mocking from outside of root #7789
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
packages/mocker/src/node/resolver.ts
Outdated
@@ -104,7 +104,14 @@ export class ServerMockResolver { | |||
|
|||
private async resolveMockId(rawId: string, importer: string) { | |||
if (!importer.startsWith(this.server.config.root)) { | |||
importer = join(this.server.config.root, importer) | |||
const resolved = await this.server.pluginContainer.resolveId( |
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. The resolveId
here seems over complicated, importer should always be resolved/partially resolved
I assume importer
here has /@fs/
or something (this is what vite does to files outside of root), so we can just strip it
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 assume
importer
here has/@fs/
or something (this is what vite does to files outside of root), so we can just strip it
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-root
test case:
root: (abs-path)/project1
id: (abs-path)/project3/lib.js
importer: (abs-path)/project3/index.js
This wasn't about /@fs
, which is already stripped out during getImporter
and stack trace parsing:
vitest/packages/utils/src/source-map.ts
Lines 69 to 72 in eeded51
if (url.startsWith('/@fs/')) { | |
const isWindows = /^\/@fs\/[a-zA-Z]:\//.test(url) | |
url = url.slice(isWindows ? 5 : 4) | |
} |
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.
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
:
{ rawId: './lib.js', importer: '/imported-test.js' }
We'll need to somehow tell mocker about whether (abs-path)/project3/index.js
was originally with /@fs
or 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 resolveId
is also fine. If you know other idea, I would be happy to hear.
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 resolveId
is overkill because we know exactly how this value is resolved already. Maybe we can just check moduleGraph.getByUrl
and return the module.file
if it exists, with a fallback to importer
without joining
v3.1.2 🐞 Bug Fixes Add global chai variable in vitest/globals (fix: #7474) - by @Jay-Karia in vitest-dev/vitest#7771 and vitest-dev/vitest#7474 (d9297) Prevent modifying test.exclude when same object passed in coverage.exclude - by @AriPerkkio in vitest-dev/vitest#7774 (c3751) Fix already hoisted mock - by @hi-ogawa in vitest-dev/vitest#7815 (773b1) Fix test.scoped inheritance - by @hi-ogawa in vitest-dev/vitest#7814 (db6c3) Remove pointer-events-none after resizing the left panel - by @alexprudhomme in vitest-dev/vitest#7811 (a7e77) Default to run mode when stdin is not a TTY - by @kentonv, @hi-ogawa and @sheremet-va in vitest-dev/vitest#7673 (6358f) Use happy-dom/jsdom types for envionmentOptions - by @hi-ogawa in vitest-dev/vitest#7795 (67430) browser: Fix transform error before browser server initialization - by @hi-ogawa in vitest-dev/vitest#7783 (5f762) Fix mocking from outside of root - by @hi-ogawa in vitest-dev/vitest#7789 (03f55) Scale iframe for non ui case - by @hi-ogawa in vitest-dev/vitest#6512 (c3374) coverage: await profiler calls - by @AriPerkkio in vitest-dev/vitest#7763 (795a6) Expose profiling timers - by @AriPerkkio in vitest-dev/vitest#7820 (5652b) deps: Update all non-major dependencies - in vitest-dev/vitest#7765 (7c3df) Update all non-major dependencies - in vitest-dev/vitest#7831 (15701) runner: Correctly call test hooks and teardown functions - by @sheremet-va in vitest-dev/vitest#7775 (3c00c) Show stacktrace on test timeout error - by @hi-ogawa in vitest-dev/vitest#7799 (df33b) ui: Load panel sizes from storage on initial load - by @userquin in vitest-dev/vitest#7265 (6555d) vite-node: Named export should overwrite export all - by @hi-ogawa in vitest-dev/vitest#7846 (5ba0d) Add ERR_MODULE_NOT_FOUND code error if module cannot be loaded - by @sheremet-va in vitest-dev/vitest#7776 (f9eac) 🏎 Performance browser: Improve browser parallelisation - by @sheremet-va in vitest-dev/vitest#7665 (816a5)
Description
It seems like a very odd use case to call
vi.mock
from outside of root, butjoin(root, importer)
also felt a bit odd. Not sure when that happens. If this is necessary, then we could also call anotherresolveId
to resolveimporter
itself. (Well, it looks like this happens with custom mock server usage like inteste/public-mocker
.)Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yaml
unless you introduce a new test example.Tests
pnpm test:ci
.Documentation
pnpm run docs
command.Changesets
feat:
,fix:
,perf:
,docs:
, orchore:
.