CtrlK
BlogDocsLog inGet started
Tessl Logo

testing-typescript

TypeScript testing with Vitest/Jest: file structure, mocking strategy, async patterns, and coverage targets

54

Quality

60%

Does it follow best practices?

Impact

No eval scenarios have been run

SecuritybySnyk

Passed

No known issues

Optimize this skill with Tessl

npx tessl skill review --optimize ./skills/testing-typescript/SKILL.md
SKILL.md
Quality
Evals
Security

Testing — TypeScript

Framework

  • Vitest for new projects (fast, native ESM, Jest-compatible API).
  • Jest only if already configured — don't mix frameworks.
  • Run: vitest run / vitest run --coverage

File Conventions

  • Co-locate: src/utils/format.tssrc/utils/format.test.ts
  • 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.
Repository
ucdavis/ai-skills-registry
Last updated
Created

Is this your skill?

If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.