DEV Community

Cover image for Writing Effective Unit Tests: Best Practices
GeekyAnts Inc
GeekyAnts Inc

Posted on

Writing Effective Unit Tests: Best Practices

Master unit testing in JavaScript with Jest. Learn AAA pattern, mocking, isolation, test coverage, edge cases, and TDD with clear, maintainable examples.

If you have ever stared at a failing test and wondered, "What is this even testing?", you're not alone.

Passing tests is only part of the story. Truly effective unit tests are readable, maintainable, and meaningful. In this post, we'll break down what makes a good unit test and how to write them well using Jest.


Table of Contents


Why Unit Tests Matter

Before diving into the how, let's talk about the why:

  • Catch bugs early: Finding issues during development is significantly cheaper than post-release.
  • Enable safe refactoring: Good tests give you confidence to change code without unintended breakage.
  • Serve as documentation: Tests explain how your code should behave.
  • Improve team collaboration: New developers understand code faster by reading tests.
  • Save money: A Microsoft study showed proper testing can reduce bug-related costs by 30–50%.

In fact, teams with strong testing practices ship features up to 30% faster, thanks to less debugging and smoother maintenance.


What You'll Learn

  • AAA pattern (Arrange, Act, Assert)
  • Test isolation & avoiding test pollution
  • Proper mocking of dependencies
  • Handling edge cases & error conditions
  • Writing maintainable, readable tests
  • Coverage metrics & goals
  • Intro to TDD (Test-Driven Development)
  • Jest-powered examples throughout

The AAA Pattern: Arrange, Act, Assert

A simple, structured way to write readable tests:

  1. Arrange: Set up test data and environment
  2. Act: Invoke the code under test
  3. Assert: Verify the result

Example with Jest

// Function to test
function add(a, b) {
  return a + b;
}

// Test
test('adds two numbers correctly', () => {
  // Arrange
  const a = 2;
  const b = 3;

  // Act
  const result = add(a, b);

  // Assert
  expect(result).toBe(5);
});
Enter fullscreen mode Exit fullscreen mode

This structure makes the test intent crystal clear.


Test Isolation & Avoiding Pollution

Each test must be independent. Shared state, side-effects, or flaky setups lead to brittle tests.

Problematic Example

let count = 0;

test('increments count', () => {
  count++;
  expect(count).toBe(1);
});

test('increments count again', () => {
  count++;
  expect(count).toBe(2); // ❌ Depends on previous test
});
Enter fullscreen mode Exit fullscreen mode

Fix with Isolation

function makeCounter() {
  let count = 0;
  return { increment: () => ++count, getCount: () => count };
}

test('increments count', () => {
  const counter = makeCounter(); // Fresh instance per test
  counter.increment();
  expect(counter.getCount()).toBe(1); // ✅ Isolated
});

test('increments count again', () => {
  const counter = makeCounter();
  counter.increment();
  expect(counter.getCount()).toBe(1); // ✅ Isolated
});
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Avoid relying on external databases, files, or services in unit tests.


Mocking Dependencies

Mocks help you isolate the unit under test by simulating external behavior.

Types of Test Doubles

Type Use Case
Mock Expect certain calls or behavior
Stub Provide canned responses
Spy Observe calls without changing behavior
Fake Lightweight implementation (e.g. in-memory DB)
Dummy Placeholder not actually used in the test

Example: Jest Mocking

// userService.js
const db = require('./db');

async function getUser(id) {
  return db.findById(id);
}

module.exports = { getUser };

// userService.test.js
const db = require('./db');
const { getUser } = require('./userService');

jest.mock('./db');

test('fetches user by id', async () => {
  // Arrange
  const mockUser = { id: 1, name: 'Alice' };
  db.findById.mockResolvedValue(mockUser);

  // Act
  const user = await getUser(1);

  // Assert
  expect(db.findById).toHaveBeenCalledWith(1);
  expect(user).toEqual(mockUser);
});
Enter fullscreen mode Exit fullscreen mode

Don't overuse mocks — they can create false confidence and tie tests to implementation details.


Testing Edge Cases & Errors

Most bugs live in edge cases. Cover them.

Boundary Values

function divide(a, b) {
  if (b === 0) throw new Error('Division by zero');
  return a / b;
}

test('divides correctly', () => {
  expect(divide(10, 2)).toBe(5);
});

test('handles zero dividend', () => {
  expect(divide(0, 5)).toBe(0);
});

test('throws on division by zero', () => {
  expect(() => divide(10, 0)).toThrow('Division by zero');
});
Enter fullscreen mode Exit fullscreen mode

Error Handling

async function fetchData(url) {
  const response = await fetch(url);
  if (!response.ok) throw new Error('Fetch failed');
  return response.json();
}

test('throws error on failed fetch', async () => {
  global.fetch = jest.fn().mockResolvedValue({ ok: false });
  await expect(fetchData('https://api.example.com')).rejects.toThrow('Fetch failed');
});
Enter fullscreen mode Exit fullscreen mode

Unexpected Inputs

test('handles null input gracefully', () => {
  expect(() => processInput(null)).toThrow();
});

test('handles empty string', () => {
  expect(processInput('')).toBe('');
});

test('handles very large numbers', () => {
  expect(add(Number.MAX_SAFE_INTEGER, 1)).toBeDefined();
});
Enter fullscreen mode Exit fullscreen mode

Testing Async Code

// Async/Await style
test('fetches user data', async () => {
  const user = await fetchUser(1);
  expect(user).toHaveProperty('name');
});

// Promise style
test('fetches user data (promise)', () => {
  return fetchUser(1).then(user => {
    expect(user).toHaveProperty('name');
  });
});

// Testing rejection
test('handles fetch error', async () => {
  await expect(fetchUser(-1)).rejects.toThrow('User not found');
});
Enter fullscreen mode Exit fullscreen mode

Testing Side Effects

test('logs an error when fetch fails', async () => {
  const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
  global.fetch = jest.fn().mockRejectedValue(new Error('Network error'));

  await fetchData('https://api.example.com').catch(() => {});

  expect(consoleSpy).toHaveBeenCalledWith(
    expect.stringContaining('Network error')
  );

  consoleSpy.mockRestore();
});
Enter fullscreen mode Exit fullscreen mode

Test Coverage

Types of Coverage

  • Line: Was each line executed?
  • Branch: Were all if/else paths run?
  • Function: Were all functions invoked?

Tips

  • Don't chase 100%
  • Focus on critical logic, not trivial code
# Run Jest with coverage
jest --coverage

# Set coverage thresholds in jest.config.js
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80,
    },
  },
};
Enter fullscreen mode Exit fullscreen mode

Writing Maintainable Tests

Readable tests = maintainable tests.

Guidelines

Descriptive test names:

// ❌ Bad
test('test1', () => { ... });

// ✅ Good
test('returns null when user is not found', () => { ... });
Enter fullscreen mode Exit fullscreen mode

Use describe blocks for organization:

describe('UserService', () => {
  describe('getUser', () => {
    test('returns user when found', () => { ... });
    test('throws error when not found', () => { ... });
  });
});
Enter fullscreen mode Exit fullscreen mode

Avoid duplication with helpers/factories:

function createUser(overrides = {}) {
  return {
    id: 1,
    name: 'Alice',
    email: 'alice@example.com',
    role: 'user',
    ...overrides,
  };
}

test('admin can access dashboard', () => {
  const admin = createUser({ role: 'admin' });
  expect(canAccess(admin, '/dashboard')).toBe(true);
});
Enter fullscreen mode Exit fullscreen mode

Embracing TDD: Red, Green, Refactor

  1. Red: Write a failing test
  2. Green: Make it pass with minimal code
  3. Refactor: Clean the implementation

Example

// Step 1: Red — Write failing test
test('capitalizes first letter of a string', () => {
  expect(capitalize('hello')).toBe('Hello');
});

// Step 2: Green — Minimal implementation
function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

// Step 3: Refactor — Handle edge cases
function capitalize(str) {
  if (!str || typeof str !== 'string') return '';
  return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
Enter fullscreen mode Exit fullscreen mode

TDD improves design, encourages modularity, and builds a natural test suite.


Bonus Tips

  • Use beforeEach/afterEach wisely
  • Keep tests focused (one test = one behavior)
  • Don't assert unrelated outcomes in one test
  • Use snapshot testing sparingly
  • Run in watch mode (jest --watch)
  • Use pre-commit hooks to enforce testing discipline
  • Prioritize clarity over cleverness

Final Thoughts

Well-written unit tests are a long-term investment — they accelerate development, reduce bugs, and improve code quality.

Stick to principles like the AAA pattern, proper mocking, isolation, and meaningful naming. And always remember:

Test code is production code — treat it with the same care.

What's your biggest challenge with writing unit tests? Drop a comment — I'd love to hear your thoughts!


Originally published on GeekyAnts Blog

Top comments (0)