Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Testing with Jest & `ts-jest` | TypeScript Mastery lesson | BraveProgrammer
  1. Courses
  2. /
  3. TypeScript Mastery

Lesson 38 of 56

·Typescript

Testing with Jest & `ts-jest`

20 minutes

Course progress68%

Install Jest for TypeScript

npm i -D jest ts-jest @types/jest
npx ts-jest config:init

Updates jest.config.js to use ts-jest transformer.

Simple test

// sum.ts
export const sum = (a: , : ) => a + b;
number
b
number
// sum.test.ts
import { sum } from './sum';

test('adds two numbers', () => {
  expect(sum(2, 3)).toBe(5);
});

Run with npm test.

Mocking modules

jest.mock('axios');
import axios from 'axios';
(axios.get as jest.Mock).mockResolvedValue({ data: { id: 1 } });

Using expect.assertions

Ensures async assertions fire.

test('async fetch', async () => {
  expect.assertions(1);
  const data = await fetchData();
  expect(data).toBe('done');
});

Info

Add "collectCoverage": true in jest.config.js to generate coverage reports.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from TypeScript MasteryBrowse activities
Previous lessonDatabase Access with TypeORMContinue to lesson 39Mocking & Spying in JestBack to TypeScript Mastery curriculum