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