Blog Thumbnail

What is Test Driven Development

MN

Makara Nuol

21-10-2020 · 3 min read

What is TDD (Test Driven Development)

Test driven Development (TDD) is a software development methodology where developers write automated tests before writing the actual code. The core principle is to write a test see it fail, write the minimum code make it pass, and then refactor.

Key points about TDD:

  • It's an Interactive approach combining programming, unit test creation, and refactoring.
  • Tests are written before the actual code implementation.
  • It follows a "Red-Green-Refactor" cycle:
    • Write a test that fails (red)
    • Write code to make the test pass (green)
    • Refactor the code (refactor)

Benefits of TDD

TDD offers several advantages:

  • Improve code quality and reliability
  • Encourages writing testable, loosely-coupled code
  • Provides quick feedback during development
  • Helps in better analysis and understanding of requirements
  • Enhances productivity and leads to more maintainable code

Basic of testing with Jest

What is Jest?

  • JavaScript / Typescript testing framework developed by facebook
  • Test runner
  • Assertion library with powerful set of matchers

Advantages of Jest:

  • Most popular test framework, most supported
  • All in one solution: (test runner, assertion library, matchers)
  • Typescript support

Project setup:

Create new nodejs project

mkdir jest-testing
code jest-test
pnpm init

Install required dependancies

pnpm i -D typescript jest ts-jest @types/jest ts-node

Initialize jest config file

npx ts-jest config:init

After initialize jest config it will create js file but we going to use typescript so let's delete jest.config.js and create new one jest.config.ts and pass below code inside

import type { Config } from "@jest/types";
 
const config: Config.InitialOptions = {
    preset: "ts-jest",
    testEnvironment: 'node',
    verbose: true
}
 
export default config;

Lets create src folder and 2 more sub folder call app and test

node-module
src
  ├─ app
  │   └── Utils.ts   
  └─ test
      └── Utils.spec.ts / Utils.test.ts
jest.config.ts
packages.json
pnpm-lock.yaml

Let's write some code inside Utils.ts file

export function toUpperCase(str: string) {
    return str.toUpperCase();
}
 
export function toLowerCase(str: string) {
    return str.toLowerCase();
}

Inside let's add some testUtils.spec.ts

import { toLowerCase, toUpperCase } from "../app/Utils";
 
describe("String utils test suite: ", () => {
    test("should return upper case string", () => {
        const result = toUpperCase("hello");
        expect(result).toBe("HELLO");
    });
 
    test("should return lower case string", () => {
        const result = toLowerCase("HELLO");
        expect(result).toBe("hello");
    })
});

Proper structure of unit test

AAA principles:

  • arrange
  • act
  • assert
import { toLowerCase, toUpperCase } from "../app/Utils";
 
describe("String utils test suite: ", () => {
    it("should return upper case of valid string", () => {
        // arrange:
        const sut = toUpperCase;
        const expected = 'HELLO';
 
        // act:
        const actual = sut('hello');
 
        // assert:
        expect(actual).toBe(expected);
    });
 
    it("should return lower case string", () => {
        const sut = toLowerCase;
        const expected = 'hello';
 
        const actual = sut('HELLO');
 
        expect(actual).toBe(expected);
    })
});