Skip to article content
FrontendXLesson 17 / 24

Test Data, Fixtures and Builders

Build a working model of automated testing from assertions to unit, component, API, integration, contract and end-to-end tests, then apply it to a single progressive task-board capstone where each milestone adds a real defect, a failing test, a corrected implementation and an expected result.

intermediate · about 35 minutes · 6 exercises

  • Predict
  • Explain
  • Implement
  • Debug
  • Test
  • Interview

What you will be able to do

#
  • Create fixtures that are minimal and readable.
  • Use builders to vary only the fields a test cares about.
  • Avoid shared mutable fixtures that leak between tests.

Overview

#

Data setup dominates test code

Most test code is setup. If setup is noisy, the assertion is hard to find and the intent is lost. Good test data is minimal, explicit and created fresh per test.

Minimal fixtures

A fixture should contain only the fields the behavior under test reads. A task object with twelve fields, where the test only checks the title, buries the relevant data. Start minimal and add fields only when a test needs them.

Builders for variation

When many tests need an object with a few different fields, a builder with defaults keeps each test focused. The test overrides only the field it cares about, so the reader sees exactly what makes this case different.

snippet.js
function makeTask(overrides = {}) {
  return { id: 1, title: 'task', status: 'todo', priority: 'medium', ...overrides };
}

const done = makeTask({ status: 'done' });

Fresh per test, never shared

A module-level fixture object that tests mutate is a flake waiting to happen. Build a fresh object inside each test, or use a factory function. If a fixture is expensive to build, build it in a hook that runs before each test, not once for the whole file.

Deterministic values

Avoid Date.now() and Math.random() in fixtures. Use fixed ids, fixed timestamps and fixed strings so the same test always sees the same data. When a test genuinely needs randomness, seed it and record the seed.

Fixtures versus factories versus builders

A fixture is a fixed example. A factory creates examples with defaults. A builder composes examples step by step. Use a fixture for a documented example, a factory for most tests, and a builder only when construction is genuinely multi-step. Do not build a builder framework for three objects.

Keep fixtures close to the test

A fixture used by one test belongs in that test. A fixture shared across a file belongs at the top of the file. A fixture shared across the whole suite belongs in a shared module, but only if it truly is shared. Premature sharing couples unrelated tests.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What does `makeTask({ status: 'done' }).priority` return?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.javascript
    function makeTask(overrides = {}) {
      return { id: 1, title: 'task', status: 'todo', priority: 'medium', ...overrides };
    }
    console.log(makeTask({ status: 'done' }).priority);
    Choose the best answer
  2. Exercise 2Explain

    Explain why a shared mutable fixture object causes flaky tests.

    Reveal the reference answer
    reference.javascript
    If a fixture object is created once and tests mutate it, one test's changes persist into the next. The second test then runs against data it did not create, so its result depends on test order and on whether other tests ran first. Creating a fresh object per test removes the shared state, so each test sees exactly the data it set up.

    The answer should connect shared mutation to order dependence and to the fix of building fresh data per test.

  3. Exercise 3Implement

    Implement `makeTask(overrides)` with defaults `{ id: 1, title: 'task', status: 'todo', priority: 'medium' }` and apply overrides. `console.log(makeTask({ id: 9 }).id, makeTask().title)` should print `9 task`.

    starter.javascript
    function makeTask(overrides = {}) {
      // your code here
    }

    Reveal the reference answer
    reference.javascript
    function makeTask(overrides = {}) {
      return { id: 1, title: 'task', status: 'todo', priority: 'medium', ...overrides };
    }

    Authored expected output: 9 task

    The defaults provide every field and the spread applies overrides last, so passing id 9 changes only the id while the title keeps its default. This is the factory pattern for test data.

  4. Exercise 4Debug

    Two tests share one module-level `task` object. The first test completes it, so the second test sees `status: 'done'` instead of its own `'todo'`. Fix the fixture so each test builds fresh data and the second test's own task stays `'todo'`, so `console.log(...)` prints `isolated`.

    starter.javascript
    const shared = { id: 1, title: 'a', status: 'todo' };
    function complete(t) {
      t.status = 'done';
      return t;
    }
    complete(shared);
    const second = shared;
    console.log(second.status === 'todo' ? 'isolated' : 'leaked');

    Reveal the reference answer
    reference.javascript
    function makeTask() {
      return { id: 1, title: 'a', status: 'todo' };
    }
    function complete(t) {
      t.status = 'done';
      return t;
    }
    const first = makeTask();
    complete(first);
    const second = makeTask();
    console.log(second.status === 'todo' ? 'isolated' : 'leaked');

    Authored expected output: isolated

    The starter reuses one shared object, so the first test's mutation leaks into the second and it prints 'leaked'. Building a fresh object per test with a factory keeps the second test's own task at 'todo', so it prints 'isolated'.

  5. Exercise 5Test

    Write two tests that each build their own task and prove the second is unaffected by the first.

    starter.javascript
    function makeTask(overrides = {}) {
      return { id: 1, title: 'task', status: 'todo', priority: 'medium', ...overrides };
    }

    Reveal the reference answer
    reference.javascript
    function assertEqual(actual, expected, label) {
      if (actual !== expected) throw new Error(label + ': expected ' + expected + ' but got ' + actual);
    }
    const first = makeTask();
    first.status = 'done';
    assertEqual(first.status, 'done', 'first mutated');
    const second = makeTask();
    assertEqual(second.status, 'todo', 'second unaffected');
    console.log('fixtures isolated');

    Authored expected output: fixtures isolated

    Each test calls the factory, so the second task is a new object with the default status. Mutating the first does not affect the second, which is the isolation the factory provides.

  6. Exercise 6Interview

    In an interview, explain how you keep test data readable as a suite grows.

    Reveal the reference answer
    reference.javascript
    I keep fixtures minimal, containing only the fields the test reads, and I use a factory with sensible defaults so each test overrides only what matters. I build fresh data per test to avoid shared state. I avoid random and time-based values so tests stay deterministic. When a fixture is used by one test I keep it inline; I only share it when it is genuinely common, and I never mutate a shared object.

    The answer should combine minimalism, a factory with overrides, per-test freshness and determinism.

Common misconceptions

#
  • A shared fixture is more efficient and therefore better.
  • Fixtures should contain every field the real object has.
  • Random data makes tests more realistic.

Follow-up practice

#
  • testing-error-and-negative-paths
  • testing-integration

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.