test-pantry

raw JSON →
1.7.1 verified Sat May 09 auth: no javascript maintenance

Test Pantry is a JavaScript test factory library for creating test data in a simple, fluid manner. Current stable version is 1.7.1. It uses a 'pantry' instance where you define recipes for objects (factories), then call them to generate instances. Key features include support for traits (mixing multiple factories), dynamic properties via functions, after-build hooks, sequence counters via `this.count`, list generation by passing a number, and alternative call syntax (e.g., `pantry.recipeName()`). Unlike libraries like Factory Bot or Rosie, Test Pantry emphasizes a minimalist JavaScript-native API with chaining and UMD support, though it has not seen recent updates and relies on UMD builds without ESM, which may cause issues in modern environments.

error TypeError: TestPantry is not a constructor
cause Calling TestPantry() without 'new' in strict ES6 or using destructured import
fix
Use 'new TestPantry()' or import default: import TestPantry from 'test-pantry'
error Cannot find module 'test-pantry'
cause Missing dev dependency or incorrect module path
fix
Run 'npm install -D test-pantry' and ensure node_modules contains it.
error TypeError: pantry.recipeFor is not a function
cause pantry not instantiated properly (e.g., pantry = TestPantry instead of new TestPantry)
fix
const pantry = new TestPantry()
gotcha Properties defined as functions in object literal factories are evaluated at creation time, not treated as values.
fix If you want a function as a value, wrap it in another function or use a function factory.
gotcha The `this.count` in function factories is per-factory, not global. Each factory has its own counter starting at 1.
fix Use a different mechanism if you need a global sequence.
deprecated Package uses UMD format; there is no ESM build. Importing in modern bundlers may require specific configuration.
fix If using ESM, consider using a bundler that can handle UMD, or look for an alternative library with ESM support.
gotcha Traits are merged shallowly. Nested objects will be overwritten, not deep merged.
fix Plan trait composition to avoid nested object conflicts.
npm install test-pantry
yarn add test-pantry
pnpm add test-pantry

Defines two factories (object literal and function) and creates single and multiple instances.

import TestPantry from 'test-pantry'

const pantry = new TestPantry()

pantry.recipeFor('user', {
  name: 'Alice',
  email: 'alice@example.com',
  role: 'admin'
})

const user = pantry('user')
console.log(user)
// { name: 'Alice', email: 'alice@example.com', role: 'admin' }

// Using function factory with sequence
pantry.recipeFor('player', function() {
  return { id: `${this.name}-${this.count}`, score: Math.random() }
})

const players = pantry(2, 'player')
console.log(players)
// [ { id: 'player-1', score: 0.123 }, { id: 'player-2', score: 0.456 } ]