Why Testing TypeScript Isn't Scary, But Beneficial
\n\nYou 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\nIn 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\nWhy test TypeScript at all if it's already safe?
\n\nTypeScript 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\nfunction sum(a: number, b: number): number {\n return a - b; // Error! Subtraction instead of addition\n}\n\nTypeScript 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.
Step 1. Choosing a Tool: Why Jest is a Beginner's Best Friend
\n\nThere 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
Let's install Jest in your project. I assume you already have TypeScript. If not, create a new project:
\n\nmkdir my-ts-project\ncd my-ts-project\nnpm init -y\nnpm install typescript jest @types/jest ts-jest --save-dev\n\nNow create a jest.config.js file in the project root:
module.exports = {\n preset: 'ts-jest',\n testEnvironment: 'node',\n roots: ['/src'],\n testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],\n}; \n\nAnd add a script to package.json:
"scripts": {\n "test": "jest",\n "test:watch": "jest --watch"\n}\n\nDone! Now you can write tests.
\n\nStep 2. Writing Your First Test in TypeScript
\n\nLet's create a simple function in the file src/math.ts:
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\nNow let's write a test for it. Create a folder src/__tests__/ and a file math.test.ts:
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\nRun npm test and see the green report. Congratulations — you've just written your first unit tests in TypeScript!
What happened here?
\n\n- \n
describe— groups tests into a logical block. \n testorit— an individual test case. \n expect— an assertion that checks