100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Testing

Test Fixtures and Factories

Learn how to build reusable test fixtures and factory functions to generate consistent, customizable test data across a Jest suite.

Setup & TeardownIntermediate9 min readJul 10, 2026
Analogies

What Are Fixtures and Factories?

A test fixture is a piece of known, reusable test data — an object, file, or state — that multiple tests rely on as a starting point. A factory is a function that generates such fixture objects programmatically, typically with sensible default values that any individual test can override.

🏏

Cricket analogy: A test fixture is like a standard practice pitch prepared to fixed specifications so every batting drill for the U-19 squad starts from the same known surface, rather than each player testing on a different random ground.

Building Factory Functions with Overrides

A well-designed factory function returns an object with realistic defaults for every field, and accepts an overrides parameter spread onto that default object so a specific test can customize only the fields it cares about while every other field stays predictable.

🏏

Cricket analogy: A createPlayer({ overrides }) factory that lets you override the strike rate is like a coach's drill generator that produces a standard net session but lets you tweak just the bowling speed to simulate facing Mitchell Starc specifically.

javascript
// fixtures/userFactory.js
let idCounter = 1;

export function createUser(overrides = {}) {
  return {
    id: idCounter++,
    name: 'Test User',
    email: `user${idCounter}@example.com`,
    role: 'member',
    createdAt: new Date('2026-01-01').toISOString(),
    ...overrides,
  };
}

// user.test.js
import { createUser } from './fixtures/userFactory';

test('admin users can delete posts', () => {
  const admin = createUser({ role: 'admin' });
  expect(canDeletePost(admin)).toBe(true);
});

test('member users cannot delete posts', () => {
  const member = createUser({ role: 'member' });
  expect(canDeletePost(member)).toBe(false);
});

Static Fixtures vs. Randomized Data

Static fixture files, like a saved sample-response.json, are ideal when a test needs exact, unchanging reference data. Libraries such as faker.js instead generate realistic but randomized data on each call, which is useful for exercising a wider range of inputs without hand-writing every variation.

🏏

Cricket analogy: A static fixture file like sample-scorecard.json is like a museum's archived scorecard from a famous match kept exactly as-is, referenced repeatedly for historical analysis without ever changing.

Factory functions are typically kept in a dedicated fixtures/ or test-utils/ directory and exported so they can be imported across many test files, keeping test data creation DRY and consistent with your actual data shapes.

Hardcoding a fixed id or email in every fixture object can cause unique-constraint violations or false test passes when multiple factory-created objects collide; incrementing counters or using faker's randomization avoids this.

  • A test fixture is a known, reusable piece of test data or state.
  • A factory function generates fixture objects programmatically, often with sensible defaults.
  • Factories accept an overrides object so individual tests can customize only what they need.
  • Static fixture files (JSON, sample data) are good for exact, unchanging reference data.
  • Libraries like faker.js generate realistic randomized data for broader test coverage.
  • Keeping factories in a shared fixtures/ directory avoids duplicated, inconsistent test data across files.
  • Unique fields (ids, emails) should be generated dynamically to avoid collisions between factory-created objects.

Practice what you learned

Was this page helpful?

Topics covered

#Testing#JestStudyNotes#TestingQA#TestFixturesAndFactories#Test#Fixtures#Factories#Building#StudyNotes#SkillVeris