1+ import assert from "node:assert/strict" ;
12import fs from "node:fs" ;
23import path from "node:path" ;
34import { PassThrough } from "node:stream" ;
@@ -244,37 +245,35 @@ export default {
244245 `
245246import fs from "node:fs";
246247import { it, expect } from "vitest";
248+ import { threadId } from "node:worker_threads";
247249${ scenario === "hung-exit" ? `process.once("exit", () => { fs.writeFileSync(${ JSON . stringify ( ready ) } , "exit"); Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0); });` : "" }
248250${ scenario === "bad-exit" ? 'process.once("exit", () => { process.exitCode = 23; });' : "" }
249251it("completes the test before worker shutdown", () => {
250- fs.writeFileSync(${ JSON . stringify ( receipt ) } , JSON.stringify({ pid: process.pid, home: process.env.HOME }));
252+ fs.writeFileSync(${ JSON . stringify ( receipt ) } , JSON.stringify({ pid: process.pid, threadId, home: process.env.HOME }));
251253 ${ fail ? 'expect.fail("intentional fixture failure");' : "expect(true).toBe(true);" }
252254});
253255` ,
254256 ) ;
255- const args =
256- scenario === "plain" || scenario === "custom"
257- ? [
258- path . join ( repo , "scripts/run-vitest.mjs" ) ,
259- "run" ,
260- "--config" ,
261- config ,
262- "--root" ,
263- root ,
264- "--configLoader" ,
265- "native" ,
266- ]
267- : [
268- path . join ( repo , "scripts/run-vitest-profile.mts" ) ,
269- "runner" ,
270- "--output-dir" ,
271- profiles ,
272- "--" ,
273- "--root" ,
274- root ,
275- "--configLoader" ,
276- "native" ,
277- ] ;
257+ const args = [
258+ path . join ( repo , "scripts/run-vitest.mjs" ) ,
259+ "run" ,
260+ "--config" ,
261+ config ,
262+ "--root" ,
263+ root ,
264+ "--configLoader" ,
265+ "native" ,
266+ ] ;
267+ if ( scenario !== "plain" && scenario !== "custom" ) {
268+ // This shutdown contract covers Node's exit-time writes, not the Inspector
269+ // profiler's awaited cleanup. Pass native flags only to the actual worker.
270+ args . push (
271+ "--execArgv=--cpu-prof" ,
272+ `--execArgv=--cpu-prof-dir=${ profiles } ` ,
273+ "--execArgv=--heap-prof" ,
274+ `--execArgv=--heap-prof-dir=${ profiles } ` ,
275+ ) ;
276+ }
278277 const { child, completion } = spawnOwnedVitestProcess ( {
279278 command : process . execPath ,
280279 args,
@@ -289,6 +288,10 @@ it("completes the test before worker shutdown", () => {
289288 output += chunk ;
290289 } ) ;
291290 const { code } = await completion . finally ( detachCleanup ) ;
291+ assert . ok (
292+ fs . existsSync ( receipt ) ,
293+ `Vitest exited before the fixture test (code ${ code } ):\n${ output } ` ,
294+ ) ;
292295 const state = JSON . parse ( fs . readFileSync ( receipt , "utf8" ) ) ;
293296 let workerStopped = false ;
294297 try {
@@ -302,6 +305,12 @@ it("completes the test before worker shutdown", () => {
302305 }
303306 const counts = { cpu : 0 , heap : 0 } ;
304307 for ( const file of fs . readdirSync ( profiles ) ) {
308+ // Native profile names contain PID/thread ID. A launcher profile cannot
309+ // establish that the worker's exit-time writes completed.
310+ const [ , , , pid , workerThreadId ] = file . split ( "." ) ;
311+ if ( pid !== String ( state . pid ) || workerThreadId !== String ( state . threadId ) ) {
312+ continue ;
313+ }
305314 const profile = JSON . parse ( fs . readFileSync ( path . join ( profiles , file ) , "utf8" ) ) ;
306315 if ( file . endsWith ( ".cpuprofile" ) && profile . nodes ?. length ) {
307316 counts . cpu ++ ;
@@ -314,6 +323,7 @@ it("completes the test before worker shutdown", () => {
314323 JSON . stringify ( {
315324 code,
316325 output,
326+ worker : { pid : state . pid , threadId : state . threadId } ,
317327 workerStopped,
318328 profiles : counts ,
319329 homeRemoved : ! fs . existsSync ( state . home ) ,
0 commit comments