Skip to content

Snapshot Testing for Configuration Objects

Easy ~25 min

What you'll build

A configuration generator module that produces app configuration objects, with snapshot tests that detect any unintended changes to the configuration structure.

Goals

  • Create a configuration generator function
  • Write snapshot tests using toMatchSnapshot()
  • Understand when snapshots update and how to review them
  • Learn to use inline snapshots with toMatchInlineSnapshot()
  • Practice reviewing snapshot diffs

Prerequisites

  • Completed Project 1 or equivalent Jest experience
  • Understanding of JavaScript objects

Steps

  1. 1

    Create the config generator

    Create configGenerator.js that produces a configuration object.

    Code
    function generateConfig(env) {
      return {
        env,
        api: {
          baseUrl: env === 'production' ? 'https://api.example.com' : 'http://localhost:3000',
          timeout: env === 'production' ? 5000 : 30000,
        },
        features: {
          darkMode: true,
          analytics: env === 'production',
        },
      };
    }
    module.exports = { generateConfig };

    Expected result

    configGenerator.js is created.

  2. 2

    Write snapshot tests

    Create configGenerator.test.js with snapshot tests for different environments.

    Code
    const { generateConfig } = require('./configGenerator');
    
    test('development config snapshot', () => {
      expect(generateConfig('development')).toMatchSnapshot();
    });
    
    test('production config snapshot', () => {
      expect(generateConfig('production')).toMatchSnapshot();
    });

    Expected result

    Tests pass and a __snapshots__ directory is created with .snap files.

  3. 3

    Review the snapshot file

    Open the generated .snap file and review the saved snapshots. Understand that these represent the expected output.

    Expected result

    You understand the snapshot file format and contents.

  4. 4

    Test snapshot change detection

    Temporarily modify the config generator (e.g., change darkMode to false). Run tests — they should fail, showing the diff. Then revert the change.

    Expected result

    Tests fail showing the exact diff, then pass after reverting.

  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 how snapshot testing works: the first run captures the output, subsequent runs compare against it. Snapshots are powerful for detecting unexpected changes in complex objects, but you must review diffs carefully before updating them.

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