|
1 | | -# @webcontainer/playwright |
| 1 | +# @webcontainer/test |
2 | 2 |
|
3 | 3 | [![Version][version-badge]][npm-url] |
4 | 4 |
|
5 | | -> Playwright utilities for testing applications in WebContainers |
| 5 | +> Utilities for testing applications in WebContainers |
6 | 6 |
|
7 | | -[Installation](#installation) | [API](#api) |
| 7 | +[Installation](#installation) | [Configuration](#configuration) | [API](#api) |
8 | 8 |
|
9 | 9 | --- |
10 | 10 |
|
11 | | -Test your applications and packages inside WebContainers using Playwright. |
| 11 | +Test your applications and packages inside WebContainers. |
12 | 12 |
|
13 | 13 | ## Installation |
14 | 14 |
|
| 15 | +Add `@webcontainer/test` to your development dependencies. |
| 16 | + |
15 | 17 | ```sh |
16 | | -$ npm install --save-dev @webcontainer/playwright |
| 18 | +$ npm install --save-dev @webcontainer/test |
17 | 19 | ``` |
18 | 20 |
|
19 | | -`@playwright/test` is required as peer dependency: |
| 21 | +Vitest is also required as peer dependency. |
20 | 22 |
|
21 | 23 | ```sh |
22 | | -$ npm install --save-dev @playwright/test |
| 24 | +$ npm install --save-dev vitest @vitest/browser |
23 | 25 | ``` |
24 | 26 |
|
25 | | -## API |
26 | | - |
27 | | -### Fixtures |
| 27 | +## Configuration |
28 | 28 |
|
29 | | -WebContainer utilities are defined as [Playwright fixtures](https://playwright.dev/docs/test-fixtures). You can import pre-defined `test()`, or import each fixture manually and extend your own `test` with each fixture. |
30 | | - |
31 | | -```ts |
32 | | -// Pre-defined test() |
33 | | -import { test } from "@webcontainer/playwright"; |
34 | | -``` |
| 29 | +Add `vitestWebcontainers` plugin in your Vitest config and enable browser mode: |
35 | 30 |
|
36 | 31 | ```ts |
37 | | -// Manual import of each fixture |
38 | | -import { |
39 | | - Editor, |
40 | | - Preview, |
41 | | - Terminal, |
42 | | - WebContainer, |
43 | | -} from "@webcontainer/playwright"; |
44 | | -import { test as base } from "@playwright/test"; |
45 | | - |
46 | | -const test = base.extend<{ |
47 | | - editor: Editor; |
48 | | - preview: Preview; |
49 | | - terminal: Terminal; |
50 | | - webcontainer: WebContainer; |
51 | | -}>({ |
52 | | - editor: async ({ page }, use) => { |
53 | | - use(new Editor(page)); |
54 | | - }, |
55 | | - preview: async ({ page }, use) => { |
56 | | - use(new Preview(page)); |
57 | | - }, |
58 | | - terminal: async ({ page }, use) => { |
59 | | - use(new Terminal(page)); |
60 | | - }, |
61 | | - webcontainer: async ({ page }, use) => { |
62 | | - use(new WebContainer(page)); |
| 32 | +import { defineConfig } from "vitest/config"; |
| 33 | +import { vitestWebcontainers } from "@webcontainer/test/plugin"; |
| 34 | + |
| 35 | +export default defineConfig({ |
| 36 | + plugins: [vitestWebcontainers()], |
| 37 | + test: { |
| 38 | + browser: { |
| 39 | + enabled: true, |
| 40 | + provider: "playwright", |
| 41 | + instances: [{ browser: "chromium" }], |
| 42 | + }, |
63 | 43 | }, |
64 | 44 | }); |
65 | | - |
66 | | -export { test }; |
67 | 45 | ``` |
68 | 46 |
|
69 | | -You can access each fixture in your test cases: |
| 47 | +## API |
| 48 | + |
| 49 | +Webcontainer utilities are exposed as [test fixtures](https://vitest.dev/guide/test-context.html#test-extend). |
70 | 50 |
|
71 | 51 | ```ts |
72 | | -import { test } from "@webcontainer/playwright"; // or your own `test` setup |
| 52 | +import { test } from "@webcontainer/test"; |
73 | 53 |
|
74 | | -test("user can open Vite TypeScript starter", async ({ |
75 | | - page, |
76 | | - editor, |
| 54 | +test("run development server inside webcontainer", async ({ |
| 55 | + webcontainer, |
77 | 56 | preview, |
78 | | - terminal, |
79 | 57 | }) => { |
80 | | - await page.goto("/"); |
| 58 | + await webcontainer.mount("path/to/project"); |
81 | 59 |
|
82 | | - await editor.getByFile("package.json", /"vite": "^6.0.11"/); |
83 | | - await editor.getByFile("src/main.ts", /<h1>Hello Vite<\/h1>/); |
| 60 | + await webcontainer.runCommand("npm", ["install"]); |
| 61 | + webcontainer.runCommand("npm", ["run", "dev"]); |
84 | 62 |
|
85 | | - await terminal.getByText("VITE v6.0.11 ready"); |
86 | | - |
87 | | - await preview.getByRole("heading", { level: 1, name: "Hello Vite" }); |
| 63 | + await preview.getByRole("heading", { level: 1, name: "Hello Vite!" }); |
88 | 64 | }); |
89 | 65 | ``` |
90 | 66 |
|
91 | | -#### Editor |
| 67 | +#### `preview` |
92 | 68 |
|
93 | | -##### `getByFile` |
| 69 | +##### `getByRole` |
94 | 70 |
|
95 | | -Get file by its name and content. |
| 71 | +Vitest's [`getByRole`](https://vitest.dev/guide/browser/locators.html#getbyrole) that's scoped to the preview window. |
96 | 72 |
|
97 | 73 | ```ts |
98 | | -async function getByFile(filename: string, content: RegExp | string): Locator; |
| 74 | +await preview.getByRole("heading", { level: 1, name: "Hello Vite!" }); |
99 | 75 | ``` |
100 | 76 |
|
101 | | -#### Preview |
| 77 | +##### `getByText` |
102 | 78 |
|
103 | | -##### `getByRole` |
| 79 | +Vitest's [`getByText`](https://vitest.dev/guide/browser/locators.html#getbytext) that's scoped to the preview window. |
104 | 80 |
|
105 | | -Playwright's [`getByRole`](https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-role) that's scoped to the preview `<iframe>`. |
| 81 | +```ts |
| 82 | +await preview.getByText("Hello Vite!"); |
| 83 | +``` |
106 | 84 |
|
107 | | -#### Terminal |
| 85 | +#### `webcontainer` |
108 | 86 |
|
109 | | -##### `getByText` |
| 87 | +##### `mount` |
110 | 88 |
|
111 | | -Playwright's [`getByText`](https://playwright.dev/docs/api/class-framelocator#frame-locator-get-by-role) that's scoped to the terminal. |
| 89 | +Mount file system inside webcontainer. |
112 | 90 |
|
113 | | -#### WebContainer |
| 91 | +Accepts a path that is relative to the [project root](https://vitest.dev/config/#root), or inlined [`FileSystemTree`](https://webcontainers.io/api#filesystemtree). |
| 92 | + |
| 93 | +```ts |
| 94 | +await webcontainer.mount("/path/to/project"); |
| 95 | +``` |
114 | 96 |
|
115 | 97 | ##### `runCommand` |
116 | 98 |
|
117 | | -Run command inside WebContainer process. |
| 99 | +Run command inside webcontainer. Returns command output. |
118 | 100 |
|
119 | 101 | ```ts |
120 | | -async function runCommand(command: string): Promise<void>; |
| 102 | +await webcontainer.runCommand("npm", ["install"]); |
| 103 | + |
| 104 | +const files = await webcontainer.runCommand("ls", ["-l"]); |
121 | 105 | ``` |
122 | 106 |
|
123 | | -[version-badge]: https://img.shields.io/npm/v/@webcontainer/playwright |
124 | | -[npm-url]: https://www.npmjs.com/package/@webcontainer/playwright |
| 107 | +[version-badge]: https://img.shields.io/npm/v/@webcontainer/test |
| 108 | +[npm-url]: https://www.npmjs.com/package/@webcontainer/test |
0 commit comments