Skip to content

Commit 2dfa5da

Browse files
committed
fix: test logging unhandled rejection from teardown
1 parent d785676 commit 2dfa5da

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/fixtures/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ export const test = base.extend<{
2424

2525
await use(webcontainer);
2626

27+
addEventListener("unhandledrejection", (event) => {
28+
if (event.reason instanceof Error) {
29+
if (event.reason.message === "Process aborted") {
30+
// TODO: Remove, debugging
31+
console.log("Webcontainer teardown error", event.reason);
32+
}
33+
}
34+
});
2735
await webcontainer.teardown();
2836
},
2937
});

src/fixtures/webcontainer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
export class WebContainer {
88
private _instancePromise?: WebContainerApi;
99
private _isReady: Promise<void>;
10+
private _onExit: (() => Promise<unknown>)[] = [];
1011

1112
constructor() {
1213
this._isReady = WebContainerApi.boot({}).then((instance) => {
@@ -47,6 +48,8 @@ export class WebContainer {
4748
}
4849

4950
async teardown() {
51+
await Promise.all(this._onExit.map((fn) => fn()));
52+
5053
this._instance.teardown();
5154
this._instancePromise = undefined;
5255
}
@@ -64,6 +67,17 @@ export class WebContainer {
6467
}),
6568
);
6669

70+
// make sure any long-living processes are terminated before teardown, e.g. "npm run dev" commands
71+
this._onExit.push(() => {
72+
// @ts-ignore -- internal
73+
if (process._process != null) {
74+
console.log("Terminating process of", { command, args });
75+
process.kill();
76+
}
77+
78+
return process.exit;
79+
});
80+
6781
await process.exit;
6882

6983
return output.trim();

0 commit comments

Comments
 (0)