@forgebook/testing

中文文档
0.1.8发布于 2026/11/01MITTypeScriptES Module构建来源已验证

Tests from a package's stories: each story and variant in the browser with axe, spills and overlays, screenshots, and each story rendered on the server.

原始使用文档
文档目录5 节
  1. Setting up the tests
  2. In the browser
  3. On the server
  4. Limits today
  5. Imports and commands

@forgebook/testing

Tests from a package's Forgebook stories, so writing a story is writing its tests:

  • in the browser (Playwright), one test per story and variant (theme, contrast, style sheets, a narrow width): it fails on an error in the page, an axe violation of WCAG 2.2 A and AA, an element that spills out of its parent, or an overlay left open past the story's frame;
  • visual, the same frames compared with reference screenshots, in the Playwright container so fonts and rendering match everywhere;
  • on the server (Vitest), each story rendered to a string, so no component reads window or document while it renders, and its controls checked against its args.

Requirements: forgebook · Vite 8 · @playwright/test 1.64+ · Vitest 5 · Docker for the visual tests · MIT licence

npm install --save-dev @forgebook/testing @playwright/test vitest

Setting up the tests

The tests run on the package's own story source (the StorySource its catalogue lists), in a folder of their own. The example below is a package whose stories are src/*.stories.tsx and whose source is stories/index.ts:

// stories/index.ts
/// <reference types="@forgebook/story-format/client" />
import type { StorySource } from "@forgebook/story-format/source";

import files from "../src/*.stories.tsx?forgebook-files";

export const source: StorySource = {
  id: "acme-ui",
  name: "Acme UI",
  files: [files],
  sheets: { required: [], theme: [], restyled: [] }
};

In the browser

Six files in test/stories/. Where the stories are, with the same patterns as the source:

// test/stories/storyFiles.ts
import { join } from "node:path";

import type { StoryFilesOptions } from "@forgebook/testing/story-files";

export const STORY_FILES: StoryFilesOptions = {
  sourceId: "acme-ui",
  name: "Acme UI",
  root: join(import.meta.dirname, "..", "..", "stories"),
  patterns: ["../src/*.stories.tsx"]
};

The folder and the ports, then the Playwright configuration and the spec:

// test/stories/testServer.ts
import type { StoryTestServer } from "@forgebook/testing/playwright";

export const STORY_SERVER: StoryTestServer = { dir: import.meta.dirname, port: 41831, browserPort: 41832 };

// test/stories/playwright.config.ts
import { storyTestConfig } from "@forgebook/testing/playwright";

import { STORY_SERVER } from "./testServer";

export default storyTestConfig(STORY_SERVER);

// test/stories/stories.spec.ts
import { storyBrowserTests } from "@forgebook/testing/browser";

import { STORY_FILES } from "./storyFiles";

storyBrowserTests(STORY_FILES);

The frame page the tests open, and the Vite configuration that serves it:

<!-- test/stories/frame.html -->
<!doctype html>
<html lang="en">
  <head><meta charset="utf-8" /><title>Stories</title></head>
  <body><div id="root"></div><script type="module" src="./frame.tsx"></script></body>
</html>
// test/stories/frame.tsx
import "@forgebook/testing/fonts.css";
import { mountFrame } from "@forgebook/react/frame";

import { source } from "../../stories/index";

const root = document.getElementById("root");
if (root !== null) void mountFrame(root, { sources: [source] });

// test/stories/vite.config.ts
import { react } from "@forgebook/react/vite";
import { storyTestViteConfig } from "@forgebook/testing/vite";

export default storyTestViteConfig(import.meta.dirname, react());

Then npx playwright install chromium once, and playwright test -c test/stories/playwright.config.ts starts the frame page and runs one test per story and variant (light, dark, both with more contrast, a narrow width, and the structure view), starting Vite through the package manager that runs them (vite in testServer.ts names another command). A story that breaks a rule fails all its variants; storyBrowserTests also takes check, a rule of the package's own run on every frame.

On the server

One test file and a Vitest configuration with the catalogue's plugin, which reads the source's ?forgebook-files imports:

// test/stories/stories.server.test.tsx
import { reactRenderer } from "@forgebook/react/renderer";
import { storyServerTests } from "@forgebook/testing/server";

import { source } from "../../stories/index";

storyServerTests(source, { renderer: reactRenderer });

// vitest.config.ts
import { react } from "@forgebook/react/vite";
import { forgebookPlugin } from "forgebook/vite";
import { defineConfig } from "vitest/config";

export default defineConfig({
  plugins: [forgebookPlugin({ framework: react() })],
  test: { include: ["test/**/*.test.tsx"] }
});

storyServerTests also takes wrap: a second pass with every story inside a decorator (a provider with a partial translation catalogue), its HTML checked.

Limits today

  • The visual tests (visual.spec.ts, visualTestConfig, startVisualBrowser) compare the frames with reference images in the Playwright container, so they need Docker; this guide does not cover them yet, their modules' TSDoc does.

Imports and commands

Import or commandWhat it is
@forgebook/testing/browserThe browser and visual tests of every story
@forgebook/testing/serverThe server-render tests of every story
@forgebook/testing/playwright, @forgebook/testing/viteThe Playwright configurations and the Vite config of the test frame page
forgebook-heightsMeasures every story's height at a few widths, for the catalogue's loading boxes
forgebook-shotOpens one story of a running catalogue and reports it with a picture

More: Forgebook's README.

版本与发布记录

@forgebook/testing 共发布 9 个版本,首次发布于 2026/10/31;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.8latest2026/11/012 天前
0.1.72026/11/012 天前
0.1.62026/11/012 天前
0.1.52026/11/012 天前
0.1.42026/10/312 天前
0.1.32026/10/313 天前

关于 @forgebook/testing 的常见问题

@forgebook/testing 是什么?

从包的故事中进行测试:浏览器中的每个故事和变体都带有斧头、溢出和覆盖、屏幕截图,每个故事都在服务器上呈现。

如何安装 @forgebook/testing?

在项目目录运行 npm install @forgebook/testing,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vite(^8.0.0)、vitest(^5.0.0)、forgebook(^0.1.7)、@playwright/test(^1.64.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 forgebook-shot、forgebook-heights,可以用 npx forgebook-shot 直接运行。

@forgebook/testing 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@forgebook/testing 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@forgebook/testing 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@forgebook/testing 可以商用吗?

@forgebook/testing 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@forgebook/testing 还在维护吗?

最新版本 0.1.8 发布于 2026/11/01(2 天前);至今共发布 9 个版本,其中过去 12 个月发布了 9 个;首次发布于 2026/10/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。