Skip to content

Your First Jest Test

This lesson walks through writing your very first Jest test from an empty project: creating a function, writing a test file, running it, and reading the output when it passes and when it fails.

Writing a Function to Test

A good first test targets a small, pure function: one that takes input and returns output with no side effects. Pure functions are the easiest starting point because there is nothing to mock and no asynchronous behavior to manage.

Below, sum.js exports a simple addition function. The companion test file imports it and checks that it behaves correctly for a couple of inputs.

// sum.js
function sum(a, b) {
  return a + b;
}
module.exports = sum;

// sum.test.js
const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

This mirrors the exact example from Jest's own "Getting Started" documentation.

Running Your Test

npx jest sum.test.js

PASS  ./sum.test.js
  ✓ adds 1 + 2 to equal 3 (2 ms)

Tests:       1 passed, 1 total
Test Suites: 1 passed, 1 total
  • A green checkmark (✓) means the test passed.
  • A red ✕ means the test failed, and Jest prints a diff of expected vs. received values.
  • "Test Suites" counts files; "Tests" counts individual test()/it() blocks.
  • Exit code 0 means all tests passed; a non-zero exit code signals failure (important for CI).

First Test Cheat Sheet

The minimum pieces every first Jest test needs.

Piece Example
Source function function sum(a, b) { return a + b; }
Export it module.exports = sum;
Import in test const sum = require('./sum');
Define the test test('description', () => { ... });
Assert the result expect(sum(1, 2)).toBe(3);
Run it npx jest

Reading a Failing Test

Seeing a failure early is valuable — it teaches you how to read Jest's output. If you change the assertion to expect the wrong value, Jest prints exactly what it expected versus what it actually received.

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(4); // intentionally wrong
});

// Output:
// expect(received).toBe(expected)
//
// Expected: 4
// Received: 3

Jest's diff output is one of its most useful features for quickly finding bugs.

Writing the Test Before the Code

Some developers prefer to write the test first, watch it fail (because the function doesn't exist yet), and then write just enough code to make it pass. This "red, green, refactor" cycle is the foundation of test-driven development and works naturally with Jest's fast watch mode.

  • Red: write a test for behavior that doesn't exist yet; watch it fail.
  • Green: write the minimum code needed to make the test pass.
  • Refactor: clean up the implementation while the test keeps you honest.

Common Mistakes

  • Forgetting module.exports (or a named export), so the test file gets undefined instead of a function.
  • Testing implementation details instead of the function's observable behavior.
  • Writing a test so trivial it can never fail meaningfully (e.g. expect(true).toBe(true)).
  • Not running the test at all before moving on — always confirm it actually executes and passes.

Key Takeaways

  • Start with a small, pure function; it's the easiest thing to test.
  • A test file imports the function, calls it, and asserts on the result with expect().
  • Jest's failure output shows an "Expected" vs "Received" diff to speed up debugging.
  • Writing tests first (red-green-refactor) is a valid and common workflow with Jest.

Pro Tip

Intentionally break a passing test once, just to see what a failure looks like. Recognizing Jest's failure format quickly will save you time for the rest of your testing career.