From 82667fd0083ce719cb3ce7fae4c72a9feb443844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 16 Apr 2025 15:04:29 +0300 Subject: [PATCH] feat: expose `TestContext` for hook usage --- .github/workflows/ci.yml | 3 +-- README.md | 12 ++++++++++++ src/fixtures/index.ts | 10 ++++++---- src/index.ts | 2 +- test/hooks.test.ts | 16 ++++++++++++++++ 5 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 test/hooks.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8ac123..a69a701 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,7 +54,6 @@ jobs: publish-preview: runs-on: ubuntu-latest - if: ${{ false }} # TODO: Set up preview deployments steps: - uses: actions/checkout@v4 @@ -70,4 +69,4 @@ jobs: run: | pnpm install pnpm build - npx pkg-pr-new publish + npx pkg-pr-new publish --compact diff --git a/README.md b/README.md index 8bc26ae..71d4086 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,18 @@ test("run development server inside webcontainer", async ({ }); ``` +To use test hooks, you can import fixture typings: + +```ts +import { test, type TestContext } from "@webcontainer/test"; +import { beforeEach } from "vitest"; + +// Mount project before each test +beforeEach(({ webcontainer }) => { + await webcontainer.mount("projects/example"); +}); +``` + #### `preview` ##### `getByRole` diff --git a/src/fixtures/index.ts b/src/fixtures/index.ts index 0538725..6ad221d 100644 --- a/src/fixtures/index.ts +++ b/src/fixtures/index.ts @@ -3,6 +3,11 @@ import { test as base } from "vitest"; import { Preview } from "./preview"; import { WebContainer } from "./webcontainer"; +export interface TestContext { + preview: Preview; + webcontainer: WebContainer; +} + /** * Pre-defined [`test()` function](https://vitest.dev/guide/test-context.html#extend-test-context) with WebContainer fixtures. * @@ -24,10 +29,7 @@ import { WebContainer } from "./webcontainer"; * }); * ``` */ -export const test = base.extend<{ - preview: Preview; - webcontainer: WebContainer; -}>({ +export const test = base.extend({ preview: async ({ webcontainer }, use) => { await webcontainer.wait(); diff --git a/src/index.ts b/src/index.ts index 9c01ec3..eb265b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export { test } from "./fixtures"; +export { test, type TestContext } from "./fixtures"; diff --git a/test/hooks.test.ts b/test/hooks.test.ts new file mode 100644 index 0000000..8713ff0 --- /dev/null +++ b/test/hooks.test.ts @@ -0,0 +1,16 @@ +import { afterEach, beforeEach, expect } from "vitest"; +import { test, type TestContext } from "../src"; + +beforeEach(({ preview, webcontainer }) => { + expect(preview.getByRole).toBeTypeOf("function"); + expect(webcontainer.mount).toBeTypeOf("function"); +}); + +afterEach(({ preview, webcontainer }) => { + expect(preview.getByRole).toBeTypeOf("function"); + expect(webcontainer.mount).toBeTypeOf("function"); +}); + +test("fixtures are available in hooks", () => { + // no-op +});