write-unit-tests

bởi tldraw

Viết unit test và integration test cho tldraw SDK. Sử dụng khi tạo test mới, thêm phạm vi kiểm thử, hoặc sửa test lỗi trong packages/editor hoặc…

npx skills add https://github.com/tldraw/tldraw --skill write-unit-tests

Writing tests

Unit and integration tests use Vitest and run from workspace directories, not the repo root.

Read a neighboring test before writing a new one — the existing suites are the specification for how we test, and they stay current in a way prose can't. Good starting points:

  • packages/tldraw/src/test/SelectTool.test.ts — tool state machine assertions
  • packages/tldraw/src/test/resizing.test.ts — pointer-driven interaction with handles
  • packages/tldraw/src/lib/shapes/arrow/ArrowShapeUtil.test.ts — shape util plus bindings
  • packages/editor/src/lib/editor/managers/ClickManager/ClickManager.test.ts — a UI-free manager
  • packages/editor/src/lib/primitives/Vec.test.ts — a plain primitive

For the available TestEditor methods, read the class itself rather than a list here: packages/tldraw/src/test/TestEditor.ts.

Which workspace

  • packages/editor — core primitives, geometry, managers, base editor behavior that must not depend on default shapes or UI.
  • packages/tldraw — anything needing default shapes or tools, which is most integration tests.

Each package has its own TestEditor, and they are not interchangeable: packages/editor/src/lib/test/TestEditor.ts has no default shapes or tools, packages/tldraw/src/test/TestEditor.ts wires up the full SDK. Import from the package you're testing in.

cd packages/tldraw && yarn test run
cd packages/tldraw && yarn test run --grep "SelectTool"
cd packages/tldraw && yarn test          # watch mode

Placement

Unit tests sit next to the file they cover (Vec.tsVec.test.ts). Cross-cutting integration tests live in packages/tldraw/src/test/. Shape and tool tests sit with the implementation, not in src/test/.

Gotchas

These are the things reading an existing test won't tell you.

Wheel and pinch events are batched. dispatch() alone won't apply them — emit a tick to flush:

editor.dispatch(wheelEvent)
editor.emit('tick', 16)

See packages/editor/src/lib/editor/Editor.test.ts for the full pattern.

toCloselyMatchObject is ours, not Vitest's. Use it instead of toMatchObject whenever floating-point geometry is involved, or tests fail on rounding noise. It takes an optional roundToNearest. Defined in packages/tldraw/src/test/TestEditor.ts.

Animation-dependent code needs the rAF stub. vi.useFakeTimers() alone isn't enough, because requestAnimationFrame isn't driven by the fake clock. Tests that animate replace it at module scope — see the top of packages/tldraw/src/lib/shapes/arrow/ArrowShapeUtil.test.ts.

Dispose the editor in afterEach. editor?.dispose() releases the reactive subscriptions and timers the editor holds; without it they leak across tests in the same file. Suites that build up shapes also tend to clear them in beforeEach so each test starts from a known page.

Always mockRestore() a vi.spyOn on the editor. Editor instances outlive individual assertions within a suite, so an unrestored spy silently changes later tests.

Conventions

  • Use createShapeId() for shape IDs so they're stable and typed.
  • Prefer comparing whole objects over field-by-field assertions when it gives a clearer failure.
  • Use editor.expectToBeIn('select.idle') for state machine assertions rather than reaching into internals.
  • @ts-expect-error is the way to assert that invalid props are rejected at the type level.

Thêm skills từ tldraw

review-docs
tldraw
Xem xét và cải thiện tài liệu với vòng lặp đánh giá song song và cải tiến lặp đi lặp lại.
official
clean-copy
tldraw
Triển khai lại nhánh hiện tại trên một nhánh mới với lịch sử commit git sạch sẽ, có chất lượng tường thuật. Sử dụng khi được yêu cầu tạo một nhánh sao chép sạch, dọn dẹp commit…
official
commit-changes
tldraw
Tạo một git commit cho các thay đổi hiện tại. Sử dụng khi được yêu cầu commit các thay đổi, tạo commit, tạo thông điệp commit, hoặc commit worktree hiện tại với…
official
dotcom-release-crew
tldraw
Đăng lên
official
dotcom-release-marketing
tldraw
Đăng tóm tắt bằng ngôn ngữ đơn giản về những gì được phát hành trong bản cập nhật tldraw.com (dotcom) tuần này lên kênh Discord của đội marketing. Dùng khi chuẩn bị…
official
issue
tldraw
Tạo và nghiên cứu một issue GitHub trong kho lưu trữ tldraw từ mô tả của người dùng. Sử dụng khi người dùng gọi issue, yêu cầu tạo issue, báo cáo lỗi,…
official
pr
tldraw
Tạo hoặc cập nhật một pull request cho nhánh hiện tại trong kho lưu trữ tldraw. Sử dụng khi người dùng gọi pr, yêu cầu tạo PR, cập nhật PR hiện có, push…
official
pr-walkthrough
tldraw
Tạo video hướng dẫn có lời tường thuật về một pull request với các slide mã và tường thuật âm thanh. Sử dụng khi được yêu cầu tạo PR walkthrough, video PR, hoặc hướng dẫn…
official