Testing Guide
Running tests
Section titled “Running tests”# All ~350 testspnpm test
# Single packagepnpm --filter @xnet/sync testpnpm --filter @xnet/data testpnpm --filter @xnet/canvas test
# Single filepnpm --filter @xnet/sync vitest run src/clock.test.ts
# Pattern matchpnpm --filter @xnet/data vitest run -t "NodeStore"
# Watch modepnpm --filter @xnet/sync test:watchTest structure
Section titled “Test structure”All tests use Vitest with the Arrange-Act-Assert pattern:
import { describe, it, expect } from 'vitest'
describe('ModuleName', () => { describe('functionName', () => { it('should do expected behavior', () => { // Arrange const input = createTestData()
// Act const result = functionName(input)
// Assert expect(result).toBe(expected) }) })})What to test
Section titled “What to test”Do test:
- Core packages (
crypto,identity,sync,data,plugins) - Pure functions and algorithms
- Edge cases and error paths
- Immutability guarantees
Don’t test:
- UI components (manual testing only)
- Electron app integration
- Styling/layout
Common patterns
Section titled “Common patterns”Generating test keys
Section titled “Generating test keys”import { generateSigningKeyPair } from '@xnet/crypto'import { generateIdentity } from '@xnet/identity'
const { publicKey, privateKey } = generateSigningKeyPair()const { identity, privateKey: signingKey } = generateIdentity()Test DIDs
Section titled “Test DIDs”// Quick typed DID for testsconst testDID = 'did:key:z6MkTest123' as DID
// Or generate a real oneconst { identity } = generateIdentity()Suppressing console output
Section titled “Suppressing console output”vi.spyOn(console, 'error').mockImplementation(() => {})vi.spyOn(console, 'warn').mockImplementation(() => {})Immutability checks
Section titled “Immutability checks”it('should not mutate the input', () => { const original = createLamportClock('did:key:alice') const time = original.time tick(original) expect(original.time).toBe(time)})Where tests live
Section titled “Where tests live”Test files are colocated with source code:
packages/sync/src/ clock.ts clock.test.ts change.ts change.test.tsOr in a __tests__ directory for packages with many test files:
packages/plugins/src/ __tests__/ registry.test.ts middleware.test.ts sandbox.test.tsCoverage
Section titled “Coverage”Tests cover core algorithms and security-critical paths. Each core package has tests for its primary exports. When adding new functionality to a core package, include tests that verify the expected behavior and edge cases.