Unit Tests

bklit/bklit-ui/.agents/skills/unit-tests

作者 bklit5d699897b2dde26374cf15501dbaadbc52cea982無授權條款1.7K 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫13 天前更新

Guardrails for adding unit tests in bklit-ui without over-testing. Use when the user mentions unit test, unit tests, tests, test coverage, add tests, write tests, vitest, jest, or asks whether something should be tested.

AI 產生的概覽

為 bklit-ui 儲存庫提供何時以及如何加入單元測試的準則,主張少而高訊號的測試。

功能
為 bklit-ui 程式碼庫提供加入單元測試的決策框架,列出哪些程式碼值得測試、哪些應該跳過。它記錄了儲存庫慣例,例如使用 Node 內建測試執行器搭配 tsx、測試檔案放在 tests 目錄下,以及建議的測試寫法。它也包含撰寫前的檢查清單、給使用者的建議方式,以及應避免的反模式。
適用情境
當有人詢問單元測試、測試覆蓋率、加入或撰寫測試、vitest 或 jest,或詢問 bklit-ui 中某段程式碼是否應被測試時使用。它旨在提出或撰寫測試之前閱讀。
執行需求
僅為說明性內容,不附帶指令碼。它提到儲存庫工具如 pnpm、Node 內建測試執行器、tsx 和 turbo,但閱讀本身不需要憑證或網路存取。

Unit Tests (bklit-ui)

Read this skill before proposing or writing tests. Default stance: fewer, higher-signal tests.


When to add tests

Add tests when they lock in behavior that is easy to break silently:

Worth testingWhy
Pure functions / modulesStable inputs → outputs; fast; no DOM
Formatters, parsers, scale math, boundsRegression on string/number output is user-visible
Codegen / export / registry helpersOutput shape is the contract
Non-obvious edge casesEmpty data, reversed ranges, clamping

Examples in this repo: chart-formatters.test.ts, highlight-segment-bounds.test.ts, animation.test.ts, apps/web/lib/studio/__tests__/*.


When NOT to add tests (unless explicitly requested)

Do not add tests just to increase coverage or “be thorough”:

SkipWhy
React component render smoke testsvisx, motion, portals → brittle; manual/docs check is cheaper
memo() / guard extractions (#65-style)Structural perf refactors; output unchanged; RTL mount assertions are heavy
Default prop passthroughformatValue = intFmt — types + formatter tests cover it
Third-party library behaviorDon’t re-test d3, visx, or React
Trivial getters / one-line wrappersNo regression signal
Snapshot entire chart SVG/JSXHigh churn, low signal

If the user asks “should we test X?” — say no when X falls in this table, and suggest a lighter alternative (pure helper test, manual check, CI build).


Repo conventions

Runner: Node built-in test runner + tsx (not Jest/Vitest unless the repo adopts them later).

bash
pnpm test              # root — turbo runs packages with a test scriptpnpm --filter @bklitui/ui testcd apps/web && pnpm test

Place tests: **/__tests__/**/*.test.ts next to the code under test.

Pattern:

ts
import assert from "node:assert/strict";import { describe, it } from "node:test";import { myFn } from "../my-module";
describe("myFn", () => {  it("handles empty input", () => {    assert.equal(myFn([]), expected);  });});

Equivalence tests (preferred for formatters): assert shared module output matches the previous inline call (e.g. toLocaleDateString with same locale/options) so tests stay timezone-safe and prove no visual regression.

New package test script: add to package.json:

json
"test": "node scripts/run-tests.mjs"

Use a small scripts/run-tests.mjs that collects *.test.ts from __tests__ and invokes node --import tsx --test — shell globs break on Linux CI. Add tsx as a devDependency if missing. Wire into root turbo.json test task; CI runs pnpm test.


Decision checklist (run before writing)

  1. Is the logic pure or extractable to pure functions? → Test that. Consider extracting first.
  2. Would a test fail on a real user-visible bug? → Good candidate.
  3. Does it need jsdom / RTL / Playwright? → Stop; justify to user or defer to manual/visual check.
  4. Did the user ask for tests? → Still apply this skill; don’t over-deliver.
  5. How many cases? → 3–10 focused cases, not exhaustive matrices.

What to tell the user

When recommending tests, be explicit:

  • Add: “Test chart-formatters.ts — pure, high regression value.”
  • Skip: “Skip component tests for the memo split — no output change; build + manual chart docs are enough.”
  • CI: Mention pnpm test in PR test plan when adding or changing tests.

Anti-patterns

  • Adding Jest/Vitest/Testing Library for a one-off without team buy-in
  • Mocking entire chart context to assert useMemo call counts
  • Testing implementation details (hook order, internal component names)
  • Duplicating type-checker work (expect(typeof x).toBe('function'))
  • Committing tests that only pass locally due to hard-coded timezone/locale strings

來源與署名

來源:bklit/bklit-ui位於.agents/skills/unit-tests提交5d69989

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架