Skip to content

Commit

Permalink
fix(vitest): add more type guards for --merge-reports
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 8, 2024
1 parent eae3bf4 commit 181c362
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/vitest/src/node/reporters/blob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises'
import { mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { parse, stringify } from 'flatted'
import { dirname, resolve } from 'pathe'
Expand Down Expand Up @@ -80,10 +80,22 @@ export async function readBlobs(
const resolvedDir = resolve(process.cwd(), blobsDirectory)
const blobsFiles = await readdir(resolvedDir)
const promises = blobsFiles.map(async (file) => {
const content = await readFile(resolve(resolvedDir, file), 'utf-8')
const fullPath = resolve(resolvedDir, file)
const stats = await stat(fullPath)
if (!stats.isFile()) {
throw new TypeError(
`vitest.mergeReports() expects all paths in "${blobsDirectory}" to be files generated by the blob reporter, but "${file}" is not a file`,
)
}
const content = await readFile(fullPath, 'utf-8')
const [version, files, errors, moduleKeys, coverage] = parse(
content,
) as MergeReport
if (!version) {
throw new TypeError(
`vitest.mergeReports() expects all paths in "${blobsDirectory}" to be files generated by the blob reporter, but "${file}" is not a valid blob file`,
)
}
return { version, files, errors, moduleKeys, coverage }
})
const blobs = await Promise.all(promises)
Expand All @@ -94,6 +106,13 @@ export async function readBlobs(
)
}

const versions = new Set(blobs.map(blob => blob.version))
if (versions.size > 1) {
throw new Error(
`vitest.mergeReports() requires all blob files to be generated by the same Vitest version, received ${Array.from(versions).join(', ')}`,
)
}

// fake module graph - it is used to check if module is imported, but we don't use values inside
const projects = Object.fromEntries(
projectsArray.map(p => [p.getName(), p]),
Expand Down

0 comments on commit 181c362

Please sign in to comment.