TypeScript Testing: A Step-by-Step Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Testing TypeScript Isn't Scary, But Beneficial

\n\n

You wrote a cool function in TypeScript, ran the project — everything works. A week later you added a new feature, and the old logic broke. Sound familiar? If so, you urgently need to become friends with testing. And don't be afraid: tests are not a boring obligation, but your personal "bug detector" that works 24/7.

\n\n

In this article, we'll break down where to start testing TypeScript as a beginner. You'll learn how to set up the environment, write your first unit test, and not get confused with types. No fluff — only practice and real code examples.

\n\n

Why test TypeScript at all if it's already safe?

\n\n

TypeScript indeed catches many errors at compile time: incorrect types, missing fields, wrong arguments. But static typing doesn't save you from logical errors. Your function can compile perfectly but produce an incorrect result. For example:

\n\n
function sum(a: number, b: number): number {\n  return a - b; // Error! Subtraction instead of addition\n}
\n\n

TypeScript won't complain — the types are correct. But a unit test will immediately show that sum(2, 2) does not equal 4. Therefore, testing TypeScript is not a whim, but a necessity for any serious project.

\n\n

Step 1. Choosing a Tool: Why Jest is a Beginner's Best Friend

\n\n

There are several popular testing frameworks on the market: Mocha, Jasmine, Vitest, Jest. For beginners, I strongly recommend Jest. Why?

\n\n
    \n
  • Works "out of the box" — no lengthy setup required.
  • \n
  • Built-in mocking mechanism and code coverage.
  • \n
  • Excellent TypeScript support via ts-jest.
  • \n
  • Clear error messages.
  • \n
\n\n

Let's install Jest in your project. I assume you already have TypeScript. If not, create a new project:

\n\n
mkdir my-ts-project\ncd my-ts-project\nnpm init -y\nnpm install typescript jest @types/jest ts-jest --save-dev
\n\n

Now create a jest.config.js file in the project root:

\n\n
module.exports = {\n  preset: 'ts-jest',\n  testEnvironment: 'node',\n  roots: ['/src'],\n  testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],\n};
\n\n

And add a script to package.json:

\n\n
"scripts": {\n  "test": "jest",\n  "test:watch": "jest --watch"\n}
\n\n

Done! Now you can write tests.

\n\n

Step 2. Writing Your First Test in TypeScript

\n\n

Let's create a simple function in the file src/math.ts:

\n\n
export function add(a: number, b: number): number {\n  return a + b;\n}\n\nexport function divide(a: number, b: number): number {\n  if (b === 0) {\n    throw new Error('Division by zero is not allowed');\n  }\n  return a / b;\n}
\n\n

Now let's write a test for it. Create a folder src/__tests__/ and a file math.test.ts:

\n\n
import { add, divide } from '../math';\n\ndescribe('Math functions', () => {\n  test('adds two numbers correctly', () => {\n    expect(add(2, 3)).toBe(5);\n    expect(add(-1, 1)).toBe(0);\n    expect(add(0, 0)).toBe(0);\n  });\n\n  test('divides two numbers', () => {\n    expect(divide(10, 2)).toBe(5);\n    expect(divide(9, 3)).toBe(3);\n  });\n\n  test('throws error when dividing by zero', () => {\n    expect(() => divide(5, 0)).toThrow('Division by zero is not allowed');\n  });\n});
\n\n

Run npm test and see the green report. Congratulations — you've just written your first unit tests in TypeScript!

\n\n

What happened here?

\n\n
    \n
  • describe — groups tests into a logical block.
  • \n
  • test or it — an individual test case.
  • \n
  • expect — an assertion that checks

Blogs

Book Recommendations