TypeScript - Testing (Jest/Vitest)

Vitest (recommended)

npm i -D vitest tsx @types/node
// math.ts
export const add = (a: number, b: number) => a + b;

// math.test.ts
import { describe, it, expect } from 'vitest';
import { add } from './math';

describe('add', () => {
  it('adds numbers', () => {
    expect(add(1,2)).toBe(3);
  });
});

Jest

  • For TS, either use ts-jest (transforms TS) or prebuild with tsc/babel.
npm i -D jest ts-jest @types/jest
npx ts-jest config:init

Typechecking tests

Run tsc --noEmit against your test files or configure an isolated tsconfig for tests.