Skip to content

Commit 0a5d816

Browse files
authoredAug 9, 2024··
fix(vitest): add more type guards for --merge-reports (#6307)
1 parent 3c04fdc commit 0a5d816

File tree

1 file changed

+24
-5
lines changed
  • packages/vitest/src/node/reporters

1 file changed

+24
-5
lines changed
 

‎packages/vitest/src/node/reporters/blob.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises'
1+
import { mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises'
22
import { existsSync } from 'node:fs'
33
import { parse, stringify } from 'flatted'
44
import { dirname, resolve } from 'pathe'
@@ -79,18 +79,37 @@ export async function readBlobs(
7979
// using process.cwd() because --merge-reports can only be used in CLI
8080
const resolvedDir = resolve(process.cwd(), blobsDirectory)
8181
const blobsFiles = await readdir(resolvedDir)
82-
const promises = blobsFiles.map(async (file) => {
83-
const content = await readFile(resolve(resolvedDir, file), 'utf-8')
82+
const promises = blobsFiles.map(async (filename) => {
83+
const fullPath = resolve(resolvedDir, filename)
84+
const stats = await stat(fullPath)
85+
if (!stats.isFile()) {
86+
throw new TypeError(
87+
`vitest.mergeReports() expects all paths in "${blobsDirectory}" to be files generated by the blob reporter, but "${filename}" is not a file`,
88+
)
89+
}
90+
const content = await readFile(fullPath, 'utf-8')
8491
const [version, files, errors, moduleKeys, coverage] = parse(
8592
content,
8693
) as MergeReport
87-
return { version, files, errors, moduleKeys, coverage }
94+
if (!version) {
95+
throw new TypeError(
96+
`vitest.mergeReports() expects all paths in "${blobsDirectory}" to be files generated by the blob reporter, but "${filename}" is not a valid blob file`,
97+
)
98+
}
99+
return { version, files, errors, moduleKeys, coverage, file: filename }
88100
})
89101
const blobs = await Promise.all(promises)
90102

91103
if (!blobs.length) {
92104
throw new Error(
93-
`vitest.mergeReports() requires at least one blob file paths in the config`,
105+
`vitest.mergeReports() requires at least one blob file in "${blobsDirectory}" directory, but none were found`,
106+
)
107+
}
108+
109+
const versions = new Set(blobs.map(blob => blob.version))
110+
if (versions.size > 1) {
111+
throw new Error(
112+
`vitest.mergeReports() requires all blob files to be generated by the same Vitest version, received\n\n${blobs.map(b => `- "${b.file}" uses v${b.version}`).join('\n')}`,
94113
)
95114
}
96115

0 commit comments

Comments
 (0)
Please sign in to comment.