write-unit-tests

작성자: tldraw

tldraw SDK에 대한 단위 테스트 및 통합 테스트를 작성합니다. packages/editor 또는...에서 새 테스트를 만들거나, 테스트 커버리지를 추가하거나, 실패하는 테스트를 수정할 때 사용합니다.

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.

tldraw의 다른 스킬

review-docs
tldraw
문서를 병렬 평가와 반복적 개선 루프를 통해 검토하고 개선합니다.
official
clean-copy
tldraw
현재 브랜치를 깔끔하고 서사적인 품질의 git 커밋 기록을 가진 새 브랜치로 재구현합니다. 깔끔한 복사 브랜치를 만들거나 커밋을 정리하라는 요청이 있을 때 사용하세요.
official
commit-changes
tldraw
현재 변경 사항에 대한 git 커밋을 생성합니다. 커밋 요청, 커밋 생성, 커밋 메시지 생성, 또는 현재 작업 트리를 커밋하라는 요청을 받았을 때 사용합니다.
official
dotcom-release-crew
tldraw
Post to the
official
dotcom-release-marketing
tldraw
이번 주 tldraw.com(dotcom) 릴리스에서 제공되는 내용을 평이한 언어로 요약하여 마케팅 팀의 Discord 채널에 게시합니다. 준비할 때 사용하세요…
official
issue
tldraw
사용자 설명을 바탕으로 tldraw 저장소에 GitHub 이슈를 생성하고 조사합니다. 사용자가 이슈를 언급하거나, 이슈 생성을 요청하거나, 버그를 신고할 때 사용합니다.
official
pr
tldraw
현재 브랜치를 tldraw 저장소에 풀 리퀘스트로 생성하거나 업데이트합니다. 사용자가 pr을 호출하거나, PR 생성을 요청하거나, 기존 PR을 업데이트하거나, 푸시할 때 사용합니다.
official
pr-walkthrough
tldraw
풀 리퀘스트의 내레이션이 포함된 비디오 워크스루를 코드 슬라이드와 오디오 내레이션으로 생성합니다. PR 워크스루, PR 비디오 또는 워크스루를 요청받았을 때 사용하세요.
official