Utilities for testing applications in WebContainers
Installation | Configuration | API
Test your applications and packages inside WebContainers.
Add @webcontainer/test to your development dependencies.
$ npm install --save-dev @webcontainer/testVitest is also required as peer dependency.
$ npm install --save-dev vitest @vitest/browserAdd vitestWebcontainers plugin in your Vitest config and enable browser mode:
import { defineConfig } from "vitest/config";
import { vitestWebcontainers } from "@webcontainer/test/plugin";
export default defineConfig({
plugins: [vitestWebcontainers()],
test: {
browser: {
enabled: true,
provider: "playwright",
instances: [{ browser: "chromium" }],
},
},
});Webcontainer utilities are exposed as test fixtures.
import { test } from "@webcontainer/test";
test("run development server inside webcontainer", async ({
webcontainer,
preview,
}) => {
await webcontainer.mount("path/to/project");
await webcontainer.runCommand("npm", ["install"]);
webcontainer.runCommand("npm", ["run", "dev"]);
await preview.getByRole("heading", { level: 1, name: "Hello Vite!" });
});Vitest's getByRole that's scoped to the preview window.
await preview.getByRole("heading", { level: 1, name: "Hello Vite!" });Vitest's getByText that's scoped to the preview window.
await preview.getByText("Hello Vite!");Mount file system inside webcontainer.
Accepts a path that is relative to the project root, or inlined FileSystemTree.
await webcontainer.mount("/path/to/project");Run command inside webcontainer. Returns command output.
await webcontainer.runCommand("npm", ["install"]);
const files = await webcontainer.runCommand("ls", ["-l"]);