@fieldia/testing

中文文档
0.36.1发布于 2026/10/28MITTypeScriptESM + CJS近 30 天下载 2363

Test the Fieldia forms in your app as people use them: fill, pick, check and press by the words people read, a field's errors and value read back — for Testing Library and Playwright — and fieldia-check, which keeps stored pages valid in CI.

原始使用文档
文档目录4 节
  1. With Testing Library (Jest, Vitest)
  2. With Playwright
  3. fieldia-check in CI
  4. License

@fieldia/testing

Test the Fieldia forms in your app as people use them: find a field by the words of its label, type in it and leave it, pick from a list, tick a box, press a button by its words — then read back what a person sees: a field's problems, its warnings, what its box shows. The same helpers for Testing Library (any DOM: jsdom or happy-dom under Jest or Vitest, or a browser) and for Playwright. And fieldia-check, one line of CI that keeps the pages your app stores valid after an upgrade.

npm install --save-dev @fieldia/testing

With Testing Library (Jest, Vitest)

import { mountViewer } from '@fieldia/viewer';
import { fieldia } from '@fieldia/testing';

test('a sign-up says what is missing', async () => {
  const host = document.body.appendChild(document.createElement('div'));
  const handle = mountViewer(host, { page, dataSource });
  await handle.drawn; // the page drawn, every part it draws from in

  const form = fieldia(host);
  await form.fill('Email', 'mona@');
  expect(form.errorsOf('Email')).toEqual(['Enter an email address, like [email protected]']);
  await form.fill('Email', '[email protected]');
  await form.pick('Country', 'Egypt');
  await form.check('I agree to the terms');
  await form.press('Submit');
  expect(form.valueOf('Email')).toBe('[email protected]');
});

Each step sends the events a person's would — focus, beforeinput for each key (so a box that refuses a key, a letter in a number, refuses it here too), input, change, then leaving the box, where Fieldia says a problem — and waits a turn of the event loop for the form. No dependency on any testing library: with @testing-library/user-event, hand it the box instead: await user.type(form.box('Email'), '[email protected]'). Under fake timers, tell the helpers how to wait: fieldia(host, { settle: () => jest.advanceTimersByTimeAsync(0) }).

HelperWhat it does
field(ref)The field's element (.fd-field). Throws, naming the fields shown, when none or several match.
box(ref)The box a person types in or picks with.
fill(ref, text)Types text in place of what the box held, a key at a time, and leaves it.
pick(ref, ...options)Chooses by their words: a list's option, a radio, exactly these boxes among several, an option of Fieldia's own list or a link's record (typed, then pressed once it shows).
check(ref, on = true)Ticks or unticks a yes-or-no box or switch.
press(name)Presses a button by its words or its name for a screen reader (Save, Submit, Add a line).
errorsOf(ref) / warningsOf(ref)The problems / warnings the field says, each in its words.
valueOf(ref)What its box shows: its text; a choice's words (null for none); the words of each box ticked among several; true/false for one box. Never the stored value — read that from handle.form.getState().values.
read(ref)All of it at once: { label, value, errors, warnings }.
labels()The words of every field's label shown.

A field is named by its label's words as people read them — without its required mark, its (?) or its lock — or, where two fields share a label, by its field: { field: 'email' }. fieldia(root) takes the element the viewer was mounted in, or a dialog, or the whole document by default.

With Playwright

import { expect, test } from '@playwright/test';
import { fieldia } from '@fieldia/testing/playwright';

test('a refused save lands on its field', async ({ page }) => {
  await page.goto('/customers/42');
  const form = fieldia(page); // or a dialog: fieldia(page.getByRole('dialog'))
  await form.fill('Phone', '+20 2 1111 2222');
  await form.press('Save');
  await expect(await form.error('Email')).toHaveText('This email is already a customer’s');
  await expect.poll(() => form.valueOf('Phone')).toBe('+20 2 1111 2222');
});

The same helpers, each step Playwright's own (fill, selectOption, check, click), so they wait as Playwright waits; field, box and error give locators for expect. A read (errorsOf, valueOf) is the moment's: wrap it in expect.poll. @playwright/test is an optional peer: the helpers take its types only.

fieldia-check in CI

npx fieldia-check pages/                       # every .json in pages/ and the folders in it
npx fieldia-check pages/ --functions vat,fx     # the names of your app's own functions its pages call
npx fieldia-check pages/ --strict               # warnings fail too

Each page is checked as Fieldia reads it — validatePage: the format's shape, every field a layout names, every expression and what it calls — and its warnings said. It prints a ✓ or ✗ for each file with what is wrong, indented, and exits 1 when one is refused (2 when asked wrongly). A page saved by an older Fieldia is read as Fieldia reads it, so the check after an upgrade says whether your stored pages still draw. In code: checkPageFiles(pageFiles(['pages/'])) from @fieldia/testing/pages.

License

MIT

版本与发布记录

@fieldia/testing 共发布 13 个版本,首次发布于 2026/10/23;过去 12 个月发布了 13 个版本。

版本发布日期距今
0.36.1latest2026/10/2817 天前
0.36.02026/10/2817 天前
0.35.32026/10/2818 天前
0.35.22026/10/2718 天前
0.35.12026/10/2619 天前
0.35.02026/10/2619 天前

关于 @fieldia/testing 的常见问题

@fieldia/testing 是什么?

Test the Fieldia forms in your app as people use them: fill, pick, check and press by the words people read, a field's errors and value read back — for Testing Library and Playwright — and fieldia-check, which keeps stored pages valid in CI.

如何安装 @fieldia/testing?

在项目目录运行 npm install @fieldia/testing,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 fieldia-check,可以用 npx fieldia-check 直接运行。

@fieldia/testing 支持 TypeScript 吗?

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

@fieldia/testing 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fieldia/testing 可以商用吗?

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

@fieldia/testing 的下载量有多少?

根据 npm 官方下载统计,@fieldia/testing 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,363 次,日均约 79 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fieldia/testing 还在维护吗?

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