Skip to content

Commit

Permalink
fix: support vitest beta (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Dec 6, 2024
1 parent d232555 commit e911134
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
26 changes: 13 additions & 13 deletions src/worker/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto'
import { existsSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'pathe'
import type { CoverageProvider, ResolvedCoverageOptions, Vitest as VitestCore } from 'vitest/node'
import type { CoverageProvider, ResolvedCoverageOptions } from 'vitest/node'
import { finalCoverageFileName } from '../constants'
import type { Vitest } from './vitest'

Expand All @@ -13,11 +13,10 @@ export class VitestCoverage {
private _config: ResolvedCoverageOptions

constructor(
private ctx: VitestCore,
private vitest: Vitest,
) {
this._config = ctx.config.coverage
const projects = new Set([...ctx.projects, ctx.getCoreWorkspaceProject()])
this._config = vitest.ctx.config.coverage
const projects = new Set([...vitest.ctx.projects, vitest.getRootTestProject()])
projects.forEach((project) => {
Object.defineProperty(project.config, 'coverage', {
get: () => {
Expand All @@ -28,7 +27,7 @@ export class VitestCoverage {
},
})
})
Object.defineProperty(ctx, 'coverageProvider', {
Object.defineProperty(vitest.ctx, 'coverageProvider', {
get: () => {
if (this.enabled)
return this._provider
Expand Down Expand Up @@ -57,7 +56,7 @@ export class VitestCoverage {
}

public async enable() {
const vitest = this.ctx
const vitest = this.vitest.ctx
this._enabled = true

const jsonReporter = this._config.reporter.find(([name]) => name === 'json')
Expand All @@ -70,7 +69,7 @@ export class VitestCoverage {
this._config.reportOnFailure = true
this._config.reportsDirectory = join(tmpdir(), `vitest-coverage-${randomUUID()}`)

this.ctx.logger.log('Running coverage with configuration:', this.config)
this.vitest.ctx.logger.log('Running coverage with configuration:', this.config)

if (!this._provider) {
// @ts-expect-error private method
Expand All @@ -89,16 +88,17 @@ export class VitestCoverage {
async waitForReport() {
if (!this.enabled)
return null
const coverage = this.ctx.config.coverage
if (!coverage.enabled || !this.ctx.coverageProvider)
const ctx = this.vitest.ctx
const coverage = ctx.config.coverage
if (!coverage.enabled || !ctx.coverageProvider)
return null
this.ctx.logger.error(`Waiting for the coverage report to generate: ${coverage.reportsDirectory}`)
await this.ctx.runningPromise
ctx.logger.error(`Waiting for the coverage report to generate: ${coverage.reportsDirectory}`)
await ctx.runningPromise
if (existsSync(coverage.reportsDirectory)) {
this.ctx.logger.error(`Coverage reports retrieved: ${coverage.reportsDirectory}`)
ctx.logger.error(`Coverage reports retrieved: ${coverage.reportsDirectory}`)
return coverage.reportsDirectory
}
this.ctx.logger.error(`Coverage reports directory not found: ${coverage.reportsDirectory}`)
ctx.logger.error(`Coverage reports directory not found: ${coverage.reportsDirectory}`)
return null
}
}
10 changes: 9 additions & 1 deletion src/worker/vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ export class Vitest implements VitestMethods {
public readonly alwaysAstCollect = false,
) {
this.watcher = new VitestWatcher(this)
this.coverage = new VitestCoverage(ctx, this)
this.coverage = new VitestCoverage(this)
}

public get collecting() {
return this.ctx.configOverride.testNamePattern?.toString() === `/${Vitest.COLLECT_NAME_PATTERN}/`
}

public getRootTestProject(): WorkspaceProject {
// vitest 3 uses getRootTestProject
if ('getRootTestProject' in this.ctx) {
return (this.ctx.getRootTestProject as () => WorkspaceProject)()
}
return this.ctx.getCoreWorkspaceProject()
}

public async collectTests(files: [projectName: string, filepath: string][]) {
const astCollect: [project: WorkspaceProject, filepath: string][] = []
const otherTests: [project: WorkspaceProject, filepath: string][] = []
Expand Down
2 changes: 1 addition & 1 deletion src/worker/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class VitestWatcher {
// eslint-disable-next-line ts/no-this-alias
const state = this
const ctx = vitest.ctx
;(ctx.getCoreWorkspaceProject().provide as <T extends keyof ProvidedContext>(key: T, value: ProvidedContext[T]) => void)('__vscode', {
;(vitest.getRootTestProject().provide as <T extends keyof ProvidedContext>(key: T, value: ProvidedContext[T]) => void)('__vscode', {
get continuousFiles() {
return state.files || []
},
Expand Down

0 comments on commit e911134

Please sign in to comment.