WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 23fc47e

Browse files
authored
feat: expose TestContext for hook usage (#9)
1 parent 4e14c3f commit 23fc47e

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ jobs:
5454

5555
publish-preview:
5656
runs-on: ubuntu-latest
57-
if: ${{ false }} # TODO: Set up preview deployments
5857

5958
steps:
6059
- uses: actions/checkout@v4
@@ -70,4 +69,4 @@ jobs:
7069
run: |
7170
pnpm install
7271
pnpm build
73-
npx pkg-pr-new publish
72+
npx pkg-pr-new publish --compact

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ test("run development server inside webcontainer", async ({
6363
});
6464
```
6565

66+
To use test hooks, you can import fixture typings:
67+
68+
```ts
69+
import { test, type TestContext } from "@webcontainer/test";
70+
import { beforeEach } from "vitest";
71+
72+
// Mount project before each test
73+
beforeEach<TextContext>(({ webcontainer }) => {
74+
await webcontainer.mount("projects/example");
75+
});
76+
```
77+
6678
#### `preview`
6779

6880
##### `getByRole`

src/fixtures/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { test as base } from "vitest";
33
import { Preview } from "./preview";
44
import { WebContainer } from "./webcontainer";
55

6+
export interface TestContext {
7+
preview: Preview;
8+
webcontainer: WebContainer;
9+
}
10+
611
/**
712
* Pre-defined [`test()` function](https://vitest.dev/guide/test-context.html#extend-test-context) with WebContainer fixtures.
813
*
@@ -24,10 +29,7 @@ import { WebContainer } from "./webcontainer";
2429
* });
2530
* ```
2631
*/
27-
export const test = base.extend<{
28-
preview: Preview;
29-
webcontainer: WebContainer;
30-
}>({
32+
export const test = base.extend<TestContext>({
3133
preview: async ({ webcontainer }, use) => {
3234
await webcontainer.wait();
3335

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { test } from "./fixtures";
1+
export { test, type TestContext } from "./fixtures";

test/hooks.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { afterEach, beforeEach, expect } from "vitest";
2+
import { test, type TestContext } from "../src";
3+
4+
beforeEach<TestContext>(({ preview, webcontainer }) => {
5+
expect(preview.getByRole).toBeTypeOf("function");
6+
expect(webcontainer.mount).toBeTypeOf("function");
7+
});
8+
9+
afterEach<TestContext>(({ preview, webcontainer }) => {
10+
expect(preview.getByRole).toBeTypeOf("function");
11+
expect(webcontainer.mount).toBeTypeOf("function");
12+
});
13+
14+
test("fixtures are available in hooks", () => {
15+
// no-op
16+
});

0 commit comments

Comments
 (0)