Skip to content

Commit

Permalink
fix(coverage): istanbul didn't process files when running in the exte…
Browse files Browse the repository at this point in the history
…nsion (#468)
  • Loading branch information
sheremet-va committed Sep 9, 2024
1 parent 5df75d0 commit 19f0304
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ export const log = {
args[args.length - 1] = args.at(-1).slice(0, process.platform === 'win32' ? -2 : -1)

const time = new Date().toLocaleTimeString()
if (process.env.NODE_ENV === 'dev') {
if (process.env.EXTENSION_NODE_ENV === 'dev') {
console[type](`[INFO ${time}]`, '[Worker]', ...args)
}
_log.appendLine(`[INFO ${time}] [Worker] ${args.join(' ')}`)
},
info: (...args: any[]) => {
if (process.env.NODE_ENV === 'dev') {
if (process.env.EXTENSION_NODE_ENV === 'dev') {
console.log(...args)
}
const time = new Date().toLocaleTimeString()
_log.appendLine(`[INFO ${time}] ${args.join(' ')}`)
},
error: (...args: any[]) => {
if (process.env.NODE_ENV === 'dev') {
if (process.env.EXTENSION_NODE_ENV === 'dev') {
console.error(...args)
}
const time = new Date().toLocaleTimeString()
Expand All @@ -39,7 +39,7 @@ export const log = {
? undefined
: (...args: string[]) => {
const time = new Date().toLocaleTimeString()
if (process.env.NODE_ENV === 'dev') {
if (process.env.EXTENSION_NODE_ENV === 'dev') {
console.log(`[${time}]`, ...args)
}
_log.appendLine(`[${time}] ${args.join(' ')}`)
Expand Down
8 changes: 5 additions & 3 deletions src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export class TestRunner extends vscode.Disposable {

const testItems = request.include || this.tree.getFileTestItems(file)
function enqueue(test: vscode.TestItem) {
log.verbose?.(`Enqueuing "${test.label}`)
log.verbose?.(`Enqueuing "${test.label}"`)
run.enqueued(test)
test.children.forEach(enqueue)
}
Expand All @@ -367,9 +367,11 @@ export class TestRunner extends vscode.Disposable {
})
}

private markSuite(_testRun: vscode.TestRun, test: vscode.TestItem, result?: TaskResult) {
if (!result)
private markSuite(testRun: vscode.TestRun, test: vscode.TestItem, result?: TaskResult) {
if (!result) {
testRun.started(test)
return
}

if (result.state === 'fail') {
// errors in a suite are stored only if it happens during discovery
Expand Down
10 changes: 7 additions & 3 deletions src/worker/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ export class VitestCoverage {
})
}

private get config(): ResolvedCoverageOptions {
public get config(): ResolvedCoverageOptions {
return {
...this._config,
enabled: this.enabled,
}
}

private get enabled() {
public get enabled() {
return this._enabled && !this.vitest.collecting
}

public get resolved() {
return !!this._provider
}

public async enable() {
const vitest = this.ctx
this._enabled = true
Expand All @@ -59,7 +63,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.ctx.logger.log('Running coverage with configuration:', this.config)

if (!this._provider) {
// @ts-expect-error private method
Expand Down
32 changes: 32 additions & 0 deletions src/worker/vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ export class Vitest implements VitestMethods {
return await this.ctx.globTestFiles(filters)
}

private coverageProcessedFiles = new Set<string>()

private invalidateTree(mod: any, seen = new Set()) {
if (seen.has(mod)) {
return
}
if (this.coverageProcessedFiles.has(mod.file)) {
return
}
this.coverageProcessedFiles.add(mod.file)
seen.add(mod)
this.ctx.server.moduleGraph.invalidateModule(mod)
mod.clientImportedModules.forEach((mod: any) => {
this.invalidateTree(mod)
})
mod.ssrImportedModules.forEach((mod: any) => {
this.invalidateTree(mod)
})
}

private async runTestFiles(files: string[], testNamePattern?: string | undefined, runAllFiles = false) {
await this.ctx.runningPromise
this.watcher.markRerun(false)
Expand All @@ -126,6 +146,18 @@ export class Vitest implements VitestMethods {
if (this.debug)
await this.globTestFiles(files)

if (this.coverage.resolved) {
files.forEach((file) => {
if (!this.coverageProcessedFiles.has(file)) {
const mod = this.ctx.server.moduleGraph.getModuleById(file)
if (mod) {
this.invalidateTree(mod)
}
this.coverageProcessedFiles.add(file)
}
})
}

await this.rerunTests(files, runAllFiles)
}

Expand Down

0 comments on commit 19f0304

Please sign in to comment.