Skip to content

Commit 72acb90

Browse files
authored
fix: handle WebContainer boot timeouts (#16)
1 parent 1944c98 commit 72acb90

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/fixtures/webcontainer.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ export class WebContainer extends FileSystem {
1616
constructor() {
1717
super();
1818

19-
this._isReady = WebContainerApi.boot({}).then((instance) => {
20-
this._instancePromise = instance;
19+
this._isReady = new Promise((resolve, reject) => {
20+
const timeout = setTimeout(() => {
21+
reject(new Error("WebContainer boot timed out in 30s"));
22+
}, 30_000);
23+
24+
WebContainerApi.boot({}).then((instance) => {
25+
clearTimeout(timeout);
26+
this._instancePromise = instance;
27+
resolve();
28+
});
2129
});
2230
}
2331

test/timeouts.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { WebContainer as WebContainerApi } from "@webcontainer/api";
2+
import { expect, test, vi } from "vitest";
3+
4+
import { WebContainer } from "../src/fixtures/webcontainer";
5+
6+
test("throws when WebContainer boot timeouts", async () => {
7+
vi.spyOn(WebContainerApi, "boot").mockReturnValue(new Promise(() => null));
8+
vi.useFakeTimers();
9+
10+
const webcontainer = new WebContainer();
11+
vi.advanceTimersByTime(30_000);
12+
13+
await expect(webcontainer.wait()).rejects.toThrowErrorMatchingInlineSnapshot(
14+
`[Error: WebContainer boot timed out in 30s]`,
15+
);
16+
});

vitest.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export default defineConfig({
55
plugins: [vitestWebcontainers()],
66

77
test: {
8+
reporters: "verbose",
9+
810
browser: {
911
enabled: true,
1012
provider: "playwright",

0 commit comments

Comments
 (0)