Guide for Test-Driven Development in the DEVS platform. Use this when asked to write tests, implement TDD, or add test coverage for lib/ and stores/ code.
Guide for Test-Driven Development in the DEVS platform. Use this when asked to write tests, implement TDD, or add test coverage for lib/ and stores/ code.
Test-Driven Development (TDD) for DEVS
TDD is mandatory for all code in src/lib/ and src/stores/. This ensures LLMs can safely enhance features without causing regressions.
TDD Workflow
Red: Write a failing test that describes the expected behavior
Green: Write the minimum code necessary to make the test pass
Refactor: Improve the code while keeping tests green
Verify: Run npm run test:coverage before committing
# Run tests in watch mode (recommended during development)
npm run test:watch
# Run all tests once
npm run test:run
# Run with coverage report
npm run test:coverage
# Run E2E tests
npm run test:e2e
Unit Test Template
import { describe, it, expect, beforeEach, afterEach, vi } from'vitest'// Import the module under testimport { myFunction, MyClass } from'@/lib/my-module'// Mock dependencies
vi.mock('@/lib/db', () => ({
db: {
entities: {
toArray: vi.fn(),
add: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
},
},
}))
describe('myFunction', () => {
beforeEach(() => {
// Reset mocks and state before each test
vi.clearAllMocks()
})
afterEach(() => {
// Cleanup after each test
})
describe('when given valid input', () => {
it('should return expected result', () => {
const result = myFunction('valid input')
expect(result).toBe('expected output')
})
it('should handle edge cases', () => {
const result = myFunction('')
expect(result).toBe('')
})
})
describe('when given invalid input', () => {
it('should throw an error', () => {
expect(() =>myFunction(nullasany)).toThrow('Invalid input')
})
})
})
describe('MyClass', () => {
letinstance: MyClassbeforeEach(() => {
instance = newMyClass()
})
it('should initialize with default values', () => {
expect(instance.value).toBe(0)
})
it('should update value correctly', () => {
instance.setValue(42)
expect(instance.value).toBe(42)
})
})