Skip to content

Commit

Permalink
fix: delay initializeDistPath + test createVitest
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed May 1, 2024
1 parent 1037511 commit 04df286
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,6 @@ export class Vitest {
async start(filters?: string[]) {
this._onClose = []

// 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')

try {
await this.initCoverageProvider()
await this.coverageProvider?.clean(this.config.coverage.clean)
Expand Down Expand Up @@ -508,7 +503,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({})
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',
},
},
],
[],
])
})

0 comments on commit 04df286

Please sign in to comment.