Skip to content

Commit

Permalink
fix: call resolveId('vitest') after buildStart (#5646)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored May 2, 2024
1 parent d8304bb commit f5faf42
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class Vitest {
this.pool = undefined
this.coverageProvider = undefined
this.runningPromise = undefined
this.distPath = undefined!
this.projectsTestFiles.clear()

const resolved = resolveConfig(this.mode, options, server.config, this.logger)
Expand All @@ -110,11 +111,6 @@ export class Vitest {

this.vitenode = new ViteNodeServer(server, this.config.server)

// if Vitest is running globally, then we should still import local vitest if possible
const projectVitestPath = await this.vitenode.resolveId('vitest')
const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir
this.distPath = join(vitestDir, 'dist')

const node = this.vitenode
this.runner = new ViteNodeRunner({
root: server.config.root,
Expand Down Expand Up @@ -508,7 +504,19 @@ export class Vitest {
await project.initializeGlobalSetup()
}

private async initializeDistPath() {
if (this.distPath)
return

// if Vitest is running globally, then we should still import local vitest if possible
const projectVitestPath = await this.vitenode.resolveId('vitest')
const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir
this.distPath = join(vitestDir, 'dist')
}

async runFiles(paths: WorkspaceSpec[], allTestsRun: boolean) {
await this.initializeDistPath()

const filepaths = paths.map(([, file]) => file)
this.state.collectPaths(filepaths)

Expand Down
5 changes: 5 additions & 0 deletions test/cli/fixtures/create-vitest/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test("basic", () => {
expect(1).toBe(1);
})
3 changes: 3 additions & 0 deletions test/cli/fixtures/create-vitest/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
5 changes: 5 additions & 0 deletions test/cli/fixtures/plugin/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test("basic", () => {
expect(1).toBe(1);
})
45 changes: 45 additions & 0 deletions test/cli/fixtures/plugin/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { defineConfig } from 'vitest/config'

function logHook(...args: unknown[]) {
((globalThis as any).__testHooks ??= []).push(args[0]);

if (process.env["LOG_HOOK"]) {
console.log(...args);
}
}

export default defineConfig({
plugins: [
{
name: "test-default",
configureServer() {
logHook("configureServer(default)")
},
buildStart() {
logHook("buildStart(default)")
},
resolveId(source) {
logHook("resolveId(default)", source)
},
transform(_code, id) {
logHook("transform(default)", id)
},
},
{
name: "test-pre",
enforce: "pre",
configureServer() {
logHook("configureServer(pre)")
},
buildStart() {
logHook("buildStart(pre)")
},
resolveId(source) {
logHook("resolveId(pre)", source)
},
transform(_code, id) {
logHook("transform(pre)", id)
},
}
]
})
28 changes: 28 additions & 0 deletions test/cli/test/create-vitest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, it, vi } from 'vitest'
import { createVitest } from 'vitest/node'

it(createVitest, async () => {
const onFinished = vi.fn()
const ctx = await createVitest('test', {
watch: false,
root: 'fixtures/create-vitest',
reporters: [
{
onFinished,
},
],
})
const testFiles = await ctx.globTestFiles()
await ctx.runFiles(testFiles, false)
expect(onFinished.mock.calls[0]).toMatchObject([
[
{
name: 'basic.test.ts',
result: {
state: 'pass',
},
},
],
[],
])
})
15 changes: 15 additions & 0 deletions test/cli/test/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, it } from 'vitest'
import { runVitest } from '../../test-utils'

it('plugin hooks', async () => {
await runVitest({ root: './fixtures/plugin' })
expect((globalThis as any).__testHooks.slice(0, 5)).toEqual(
[
'configureServer(pre)',
'configureServer(default)',
'buildStart(pre)',
'buildStart(default)',
'resolveId(pre)',
],
)
})

0 comments on commit f5faf42

Please sign in to comment.