01 / 20

Jest Testing
Framework

Master the fundamentals of automated testing with describe, it, and expect: the building blocks of reliable JavaScript code.

JavaScript

What Is Automated Testing?

Automated testing means writing code that checks your other code. Instead of manually clicking through your app to see if it works, you write small programs (called tests) that run your functions and verify the results are correct.

Why Jest?

Zero Config
Jest works out of the box with most JavaScript projects: no complicated setup required.
Fast Feedback
Tests run in parallel and Jest only re-runs tests related to changed files.
Rich Matchers
Built-in assertions like toBe, toEqual, and toThrow make tests expressive.

The Three Pillars of Jest

describe("Calculator") it("adds two numbers") expect(add(2, 3)).toBe(5) it("subtracts two numbers") expect(subtract(5, 2)).toBe(3) GROUP TEST ASSERT

describe: Grouping Your Tests

describe creates a labeled block that groups related test cases together. It takes two arguments: a string describing the group and a callback function containing the tests.

describe("MathUtils", () => { // all MathUtils tests live here });

it: Defining a Single Test

Each it block (also available as test) represents one specific behavior you are verifying. The string argument should read like a sentence: "it does something."

it("returns the sum of two numbers", () => { const result = add(2, 3); expect(result).toBe(5); });

expect: Making Assertions

expect wraps a value so you can chain a matcher to test it. Matchers are methods like .toBe() or .toEqual() that compare the actual value to an expected value.

.toBe()
Strict equality (===). Use for primitives: numbers, strings, booleans.
.toEqual()
Deep equality. Use for objects and arrays where structure matters.
.toBeTruthy()
Passes if the value is truthy (not false, 0, null, undefined, NaN, "").

toBe vs toEqual

.toBe() .toEqual()
Comparison Strict reference (===) Deep structural
Use for Primitives Objects & arrays
{a:1} === {a:1} ✕ Fails ✓ Passes
5 === 5 ✓ Passes ✓ Passes
Performance Faster Slightly slower

Writing Your First Test: Step by Step

1
Create a test file. Name it myModule.test.js (Jest auto-detects files ending in .test.js).
2
Import the function you want to test at the top of the file.
3
Wrap tests in describe() with a label that matches the module or function name.
4
Write it() blocks describing each behavior: one assertion per test is ideal.
5
Run the tests with npx jest and read the output for pass/fail results.

Example 1: Testing an add Function

// math.js function add(a, b) { return a + b; } module.exports = { add };
// math.test.js const { add } = require("./math"); describe("add", () => { it("returns 5 for 2 + 3", () => { expect(add(2, 3)).toBe(5); }); });

Example 2: Testing Objects with toEqual

// user.js function createUser(name, age) { return { name, age, active: true }; } module.exports = { createUser };
// user.test.js describe("createUser", () => { it("returns a user object", () => { expect(createUser("Ada", 16)).toEqual({ name: "Ada", age: 16, active: true }); }); });

Example 3: Testing Edge Cases

describe("divide", () => { it("divides 10 by 2", () => { expect(divide(10, 2)).toBe(5); }); it("throws on division by zero", () => { expect(() => divide(10, 0)).toThrow( "Cannot divide by zero" ); }); });

More Useful Matchers

Matcher What it checks Example
.toContain() Array/string includes item expect([1,2]).toContain(1)
.toHaveLength() Array/string length expect("hi").toHaveLength(2)
.toBeNull() Value is null expect(null).toBeNull()
.toBeDefined() Value is not undefined expect(x).toBeDefined()
.not Negates any matcher expect(3).not.toBe(5)

How a Test Runs

npx jest Find *.test.js files Execute describe() blocks Run each it() + expect() PASS or FAIL reported

Common Mistakes

✕ toBe on Objects
Using .toBe() to compare objects checks reference equality, not structure. Two identical objects are different references: the test fails.
✕ Missing Arrow in toThrow
Writing expect(fn()).toThrow() calls fn immediately, crashing before Jest can catch it. Wrap in an arrow: expect(() => fn()).
✕ No Assertions at All
An it() block with no expect() always passes silently. Jest won't warn you: the test looks green but proves nothing.

Quick Reference Cheat Sheet

Concept Syntax Purpose
Group tests describe("name", fn) Organize related tests
Single test it("does X", fn) Define one behavior
Assertion expect(val).matcher() Verify a result
Exact match .toBe(expected) Primitives (===)
Deep match .toEqual(expected) Objects & arrays
Negate .not.matcher() Invert any assertion
Error check .toThrow(msg?) Verify error thrown

Question 1 of 5

What is the purpose of describe() in Jest?

Question 2 of 5

Which matcher should you use to compare two objects with the same properties?

Question 3 of 5

What is wrong with this code?
expect(divide(10, 0)).toThrow("error")

Question 4 of 5

In Jest, it() and test() are:

Question 5 of 5

What happens if an it() block contains no expect() calls?