Skip to content

Commit

Permalink
fix(vitest): always resolve vitest to the root version (#6369)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Aug 20, 2024
1 parent 5388f0c commit 163d762
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
2 changes: 0 additions & 2 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
hijackVitePluginInject,
resolveFsAllow,
} from './utils'
import { VitestResolver } from './vitestResolver'
import { VitestOptimizer } from './optimizer'
import { NormalizeURLPlugin } from './normalizeURL'

Expand Down Expand Up @@ -256,7 +255,6 @@ export async function VitestPlugin(
CoverageTransform(ctx),
options.ui ? await UIPlugin() : null,
...MocksPlugins(),
VitestResolver(ctx),
VitestOptimizer(),
NormalizeURLPlugin(),
].filter(notNullish)
Expand Down
14 changes: 9 additions & 5 deletions packages/vitest/src/node/plugins/vitestResolver.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { Plugin } from 'vite'
import { join } from 'pathe'
import type { Vitest } from '../core'

export function VitestResolver(ctx: Vitest): Plugin {
return {
const plugin: Plugin = {
name: 'vitest:resolve-root',
enforce: 'pre',
async resolveId(id) {
async resolveId(id, _, { ssr }) {
if (id === 'vitest' || id.startsWith('@vitest/')) {
return this.resolve(id, join(ctx.config.root, 'index.html'), {
skipSelf: true,
// always redirect the request to the root vitest plugin since
// it will be the one used to run Vitest
const resolved = await ctx.server.pluginContainer.resolveId(id, undefined, {
skip: new Set([plugin]),
ssr,
})
return resolved
}
},
}
return plugin
}
3 changes: 3 additions & 0 deletions test/workspaces/space_3/fake-vitest/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.defineProject = (c) => {
return c
}
1 change: 1 addition & 0 deletions test/workspaces/space_3/fake-vitest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('should not import from fake vitest')
8 changes: 8 additions & 0 deletions test/workspaces/space_3/fake-vitest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "vitest",
"type": "commonjs",
"exports": {
".": "./index.js",
"./config": "./config.js"
}
}
8 changes: 7 additions & 1 deletion test/workspaces/space_3/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"name": "@vitest/space_3",
"private": true
"private": true,
"scripts": {
"test": "vitest"
},
"dependencies": {
"vitest": "link:./fake-vitest"
}
}

0 comments on commit 163d762

Please sign in to comment.