Skip to content

Commit eb541a7

Browse files
committed
fix: fix external
1 parent 6222185 commit eb541a7

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,24 @@ export class BlobReporter implements Reporter {
101101
})
102102
})
103103

104+
const externalized: SerializedExternalizedByProject = {}
105+
this.ctx.projects.forEach((project) => {
106+
externalized[project.name] = []
107+
for (const [k, v] of project._resolver.externalizeCache.entries()) {
108+
if (typeof v === 'string') {
109+
externalized[project.name].push([k, v])
110+
}
111+
}
112+
})
113+
104114
const report = [
105115
this.ctx.version,
106116
files,
107117
errors,
108118
coverage,
109119
executionTime,
110120
environmentModules,
121+
externalized,
111122
] satisfies MergeReport
112123

113124
const reportFile = resolve(this.ctx.config.root, outputFile)
@@ -145,15 +156,15 @@ export async function readBlobs(
145156
)
146157
}
147158
const content = await readFile(fullPath, 'utf-8')
148-
const [version, files, errors, coverage, executionTime, environmentModules] = parse(
159+
const [version, files, errors, coverage, executionTime, environmentModules, externalized] = parse(
149160
content,
150161
) as MergeReport
151162
if (!version) {
152163
throw new TypeError(
153164
`vitest.mergeReports() expects all paths in "${blobsDirectory}" to be files generated by the blob reporter, but "${filename}" is not a valid blob file`,
154165
)
155166
}
156-
return { version, files, errors, coverage, file: filename, executionTime, environmentModules }
167+
return { version, files, errors, coverage, file: filename, executionTime, environmentModules, externalized }
157168
})
158169
const blobs = await Promise.all(promises)
159170

@@ -183,6 +194,16 @@ export async function readBlobs(
183194
)
184195

185196
blobs.forEach((blob) => {
197+
Object.entries(blob.externalized).forEach(([projectName, entries]) => {
198+
const project = projects[projectName]
199+
if (!project) {
200+
return
201+
}
202+
entries.forEach(([id, externalized]) => {
203+
project._resolver.externalizeCache.set(id, externalized)
204+
})
205+
})
206+
186207
Object.entries(blob.environmentModules).forEach(([projectName, environments]) => {
187208
const project = projects[projectName]
188209
if (!project) {
@@ -284,6 +305,7 @@ type MergeReport = [
284305
coverage: unknown,
285306
executionTime: number,
286307
environmentModules: MergeReportEnvironmentModules,
308+
externalized: SerializedExternalizedByProject,
287309
]
288310

289311
interface MergeReportEnvironmentModules {
@@ -295,6 +317,10 @@ interface MergeReportEnvironmentModules {
295317
}
296318
}
297319

320+
interface SerializedExternalizedByProject {
321+
[projectName: string]: [id: string, externalized: string][]
322+
}
323+
298324
type SerializedEnvironmentModuleNode = [
299325
id: number,
300326
file: number,

packages/vitest/src/node/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { isWindows } from '../utils/env'
1212
export class VitestResolver {
1313
public readonly options: ExternalizeOptions
1414
private externalizeConcurrentCache = new Map<string, Promise<string | false | undefined>>()
15-
private externalizeCache = new Map<string, string | false | undefined>()
15+
public externalizeCache: Map<string, string | false | undefined> = new Map<string, string | false | undefined>()
1616

1717
constructor(cacheDir: string, config: ResolvedConfig) {
1818
// sorting to make cache consistent
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { test, expect } from 'vitest'
22
import { hello } from './util'
3-
// import * as obug from "obug"
3+
import * as obug from "obug"
44

55
test('also passes', () => {
66
expect(hello()).toBe('Hello, graph!')
77
})
88

9-
// test('external', () => {
10-
// expect(obug).toBeTypeOf('object')
11-
// })
9+
test('external', () => {
10+
expect(obug).toBeTypeOf('object')
11+
})

test/cli/test/reporters/merge-reports.test.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ test('total and merged execution times are shown', async () => {
256256
file.tasks.push(createTest('some test', file))
257257

258258
await writeBlob(
259-
[version, [file], [], undefined, 1500 * index, {}],
259+
[version, [file], [], undefined, 1500 * index, {}, {}],
260260
resolve(`./fixtures/reporters/merge-reports/.vitest-reports/blob-${index}-2.json`),
261261
)
262262
}
@@ -279,8 +279,8 @@ test('module graph available', async () => {
279279
const reportsDir = resolve(root, '.vitest-reports')
280280
rmSync(reportsDir, { force: true, recursive: true })
281281

282-
async function getModuleGraphs(ctx: Vitest) {
283-
const files = ctx.state.getFiles().slice().sort((a, b) => a.filepath.localeCompare(b.filepath))
282+
async function getSerializedModuleGraph(ctx: Vitest) {
283+
const files = ctx.state.getFiles().slice().sort()
284284
const moduleGraphs = Object.fromEntries(
285285
await Promise.all(
286286
files.map(async (file) => {
@@ -296,7 +296,9 @@ test('module graph available', async () => {
296296
}),
297297
),
298298
)
299-
return JSON.stringify(moduleGraphs, null, 2).replaceAll(ctx.config.root, '<root>')
299+
return JSON.stringify(moduleGraphs, null, 2)
300+
.replaceAll(ctx.config.root, '<root>')
301+
.replace(/"\/[^"\n]*\/node_modules\//g, '"<node_modules>/')
300302
}
301303

302304
// generate blob
@@ -305,7 +307,7 @@ test('module graph available', async () => {
305307
reporters: ['blob'],
306308
})
307309
expect.assert(result.ctx)
308-
const generatedModuleGraphJson = await getModuleGraphs(result.ctx)
310+
const generatedModuleGraphJson = await getSerializedModuleGraph(result.ctx)
309311
expect(generatedModuleGraphJson).toMatchInlineSnapshot(`
310312
"{
311313
"<root>/basic.test.ts": {
@@ -337,10 +339,13 @@ test('module graph available', async () => {
337339
"<root>/sub/subject.ts"
338340
],
339341
"<root>/second.test.ts": [
340-
"<root>/util.ts"
342+
"<root>/util.ts",
343+
"<node_modules>/obug/dist/node.js"
341344
]
342345
},
343-
"externalized": [],
346+
"externalized": [
347+
"<node_modules>/obug/dist/node.js"
348+
],
344349
"inlined": [
345350
"<root>/second.test.ts",
346351
"<root>/util.ts",
@@ -357,7 +362,7 @@ test('module graph available', async () => {
357362
})
358363
expect(result2.stderr).toMatchInlineSnapshot(`""`)
359364
expect.assert(result2.ctx)
360-
const restoredModuleGraphJson = await getModuleGraphs(result2.ctx)
365+
const restoredModuleGraphJson = await getSerializedModuleGraph(result2.ctx)
361366
expect(restoredModuleGraphJson).toBe(generatedModuleGraphJson)
362367

363368
// also check html reporter doesn't crash

0 commit comments

Comments
 (0)