Skip to content
Home Tutorials Roadmaps Courses Classroom Training Blog
Log in Join free
Node.js Intermediate Free

Unit Testing

Lesson 11 of 17 · Testing & Debugging 65%
1. Getting Started
What is Node.js?
2. Modules & NPM
Modules & NPM
6. Authentication & Security
JWT Authentication Password Hashing
8. WebSockets & Queues
Socket.io Message Queues
9. Process & Environment
Process & Environment Variables
10. Caching & Performance
Redis Caching
11. GraphQL with Node.js
GraphQL Basics

Unit tests verify individual functions or components in isolation.

Syntax

NODEJS
const assert = require('assert');

function add(a, b) { return a + b; }

assert.strictEqual(add(1, 2), 3);
assert.strictEqual(add(-1, 1), 0);
Unit Testing with Jest
NODE
// math.js
function add(a, b) { return a + b; }
function divide(a, b) {
    if (b === 0) throw new Error("Cannot divide by zero");
    return a / b;
}

// math.test.js
// describe("Math", () => {
//     test("add", () => {
//         expect(add(2, 3)).toBe(5);
//     });
//     test("divide", () => {
//         expect(divide(10, 2)).toBe(5);
//     });
//     test("divide by zero", () => {
//         expect(() => divide(1, 0)).toThrow();
//     });
// });

Up next

Debugging with Node

Lesson 12 of 17 · Testing & Debugging