Shared fixtures: tests/fixtures/ or src/__tests__/helpers/
Test Structure
describe('formatDate', () => {
it('should return ISO date string when given a valid Date', () => {
// Arrange
const date = new Date('2026-01-15');
// Act
const result = formatDate(date);
// Assert
expect(result).toBe('2026-01-15');
});
});
Mocking
// Module mock — top of file
vi.mock('../services/emailService');
// Spy without full mock
const spy = vi.spyOn(mailer, 'send').mockResolvedValue(undefined);
// Restore after each test
afterEach(() => vi.restoreAllMocks());
// Time control
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());
Async Tests
// Always await; use assertions count for safety
it('should reject with AuthError when token is expired', async () => {
expect.assertions(1);
await expect(verifyToken('expired')).rejects.toThrow('Token expired');
});
Coverage Targets
Business logic: 85%+ branch coverage.
Utility functions: 100% line coverage.
Do not write tests just to hit numbers — test behavior.
1---2name: ucdavis-ai-skills-registry-testing-typescript3description: Testing — TypeScript4---56# Testing — TypeScript78## Framework9- **Vitest** for new projects (fast, native ESM, Jest-compatible API).10- **Jest** only if already configured — don't mix frameworks.11- Run: `vitest run` / `vitest run --coverage`1213## File Conventions14- Co-locate: `src/utils/format.ts` → `src/utils/format.test.ts`15- Shared fixtures: `tests/fixtures/` or `src/__tests__/helpers/`1617## Test Structure18```typescript19describe('formatDate', () => {20 it('should return ISO date string when given a valid Date', () => {21 // Arrange22 const date = new Date('2026-01-15');2324 // Act25 const result = formatDate(date);2627 // Assert28 expect(result).toBe('2026-01-15');29 });30});31```3233## Mocking34```typescript35// Module mock — top of file36vi.mock('../services/emailService');3738// Spy without full mock39const spy = vi.spyOn(mailer, 'send').mockResolvedValue(undefined);4041// Restore after each test42afterEach(() => vi.restoreAllMocks());4344// Time control45beforeEach(() => vi.useFakeTimers());46afterEach(() => vi.useRealTimers());47```4849## Async Tests50```typescript51// Always await; use assertions count for safety52it('should reject with AuthError when token is expired', async () => {53 expect.assertions(1);54 await expect(verifyToken('expired')).rejects.toThrow('Token expired');55});56```5758## Coverage Targets59- Business logic: 85%+ branch coverage.60- Utility functions: 100% line coverage.61- Do not write tests just to hit numbers — test behavior.6263---64> Source: [ucdavis/ai-skills-registry](https://github.com/ucdavis/ai-skills-registry) — distributed by [TomeVault](https://tomevault.io).65<!-- tomevault:4.0:skill_md:2026-06-16 -->
Run npx skillmds@latest add tomevault-io/ucdavis-ai-skills-registry-testing-typescript in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Testing — TypeScript It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.