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 && pnpm test run
cd packages/tldraw && pnpm test run --grep "SelectTool"
cd packages/tldraw && pnpm test          # watch mode

Placement

Unit tests sit next to the file they cover (Vec.ts → Vec.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 की और Skills

commit-changes
tldraw
वर्तमान परिवर्तनों के लिए git कमिट बनाएँ। जब कमिट करने, कमिट बनाने, कमिट संदेश उत्पन्न करने, या वर्तमान वर्कट्री को कमिट करने के लिए कहा जाए तो उपयोग करें…
dotcom-release-crew
tldraw
पोस्ट करें
clean-copy
tldraw
वर्तमान शाखा को एक नई शाखा पर स्वच्छ, कथात्मक-गुणवत्ता वाले गिट कमिट इतिहास के साथ पुनः कार्यान्वित करें। जब स्वच्छ कॉपी शाखा बनाने, कमिट साफ करने के लिए कहा जाए तो उपयोग करें।
dotcom-release-marketing
tldraw
इस सप्ताह के tldraw.com (dotcom) रिलीज़ में जो कुछ भी शामिल है, उसका सरल भाषा में सारांश मार्केटिंग टीम के Discord चैनल पर पोस्ट करें। इसका उपयोग तब करें जब आप तैयारी कर रहे हों…
issue
tldraw
उपयोगकर्ता के विवरण से tldraw रिपॉजिटरी में GitHub issue बनाएँ और उस पर शोध करें। इसका उपयोग तब करें जब उपयोगकर्ता issue का उल्लेख करे, issue बनाने के लिए कहे, बग रिपोर्ट करे,…
pr
tldraw
वर्तमान शाखा के लिए tldraw रिपॉजिटरी में एक पुल रिक्वेस्ट बनाएँ या अपडेट करें। इसका उपयोग तब करें जब उपयोगकर्ता pr को आमंत्रित करे, PR बनाने, मौजूदा PR को अपडेट करने, पुश करने के लिए कहे…
take
tldraw
tldraw/tldraw में एक GitHub इश्यू खोजें, उसे असाइन करें, लागू करें, सत्यापित करें और एक पुल रिक्वेस्ट खोलें। इसका उपयोग तब करें जब उपयोगकर्ता take को आमंत्रित करे, कोई इश्यू लेने के लिए कहे,…
tldraw-migrate
tldraw
किसी प्रोजेक्ट को tldraw SDK के नए संस्करण में माइग्रेट करें। tldraw पैकेज अपग्रेड करते समय, tldraw अपग्रेड के बाद TypeScript त्रुटियों को ठीक करते समय, या जब उपयोगकर्ता…