Skip to content

Integration Testing with a Real Database

Medium ~45 min

What you'll build

A user repository module that interacts with a database, with integration tests using an in-memory database to verify the full data flow.

Goals

  • Create a repository pattern with CRUD operations
  • Use an in-memory database for integration tests
  • Set up and tear down the database between tests
  • Test the full data flow from repository to storage
  • Distinguish unit tests from integration tests

Prerequisites

  • Completed Project 1 and 2 or equivalent experience
  • Understanding of CRUD operations
  • Basic knowledge of repository pattern

Steps

  1. 1

    Create the in-memory database

    Create a simple in-memory database module for testing.

    Code
    // inMemoryDb.js
    class InMemoryDb {
      constructor() { this.data = new Map(); }
      insert(id, record) { this.data.set(id, record); return record; }
      findById(id) { return this.data.get(id); }
      update(id, record) { this.data.set(id, record); return record; }
      delete(id) { return this.data.delete(id); }
      clear() { this.data.clear(); }
    }
    module.exports = { InMemoryDb };

    Expected result

    In-memory database is created.

  2. 2

    Create the user repository

    Create a UserRepository that uses the database for CRUD operations.

    Code
    class UserRepository {
      constructor(db) { this.db = db; }
      create(user) { return this.db.insert(user.id, user); }
      findById(id) { return this.db.findById(id); }
      update(id, updates) {
        const existing = this.db.findById(id);
        if (!existing) throw new Error('User not found');
        return this.db.update(id, { ...existing, ...updates });
      }
      delete(id) { return this.db.delete(id); }
    }
    module.exports = { UserRepository };

    Expected result

    UserRepository is created.

  3. 3

    Write integration tests

    Create integration tests that use the real in-memory database, with setup/teardown to reset state.

    Code
    const { InMemoryDb } = require('./inMemoryDb');
    const { UserRepository } = require('./userRepository');
    
    describe('UserRepository integration', () => {
      let db, repo;
      beforeEach(() => { db = new InMemoryDb(); repo = new UserRepository(db); });
    
      test('create and find user', () => {
        repo.create({ id: 1, name: 'Alice' });
        expect(repo.findById(1)).toEqual({ id: 1, name: 'Alice' });
      });
    
      test('update user', () => {
        repo.create({ id: 1, name: 'Alice' });
        repo.update(1, { name: 'Bob' });
        expect(repo.findById(1).name).toBe('Bob');
      });
    });

    Expected result

    All integration tests pass.

  4. 4

    Test error scenarios

    Add tests for updating and deleting non-existent users.

    Code
    test('throws when updating non-existent user', () => {
      expect(() => repo.update(999, { name: 'X' })).toThrow('User not found');
    });

    Expected result

    Error tests pass.

  5. 5

    Add boundary test cases

    Add boundary-value cases to the test suite: empty array, zero, maximum value, null/undefined. Boundaries are where bugs most often hide, so they must be explicitly covered.

    Code
    describe("boundary cases", () => {
      it("handles empty array", () => {
        expect(func([])).toEqual([]);
      });
    
      it("handles zero", () => {
        expect(func(0)).toBe(0);
      });
    
      it("handles null", () => {
        expect(() => func(null)).toThrow();
      });
    
      it("handles max value", () => {
        expect(func(Number.MAX_SAFE_INTEGER)).toBeDefined();
      });
    });

    Expected result

    All boundary cases pass with no uncaught exceptions.

Try it yourself

What you learned

You learned the difference between unit tests (mocked dependencies) and integration tests (real dependencies). Integration tests verify that components work together correctly, catching issues like data format mismatches that unit tests with mocks might miss.

Related Resources

Related Lessons

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic