test-pantry
raw JSON →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.
Common errors
error TypeError: TestPantry is not a constructor ↓
error Cannot find module 'test-pantry' ↓
error TypeError: pantry.recipeFor is not a function ↓
Warnings
gotcha Properties defined as functions in object literal factories are evaluated at creation time, not treated as values. ↓
gotcha The `this.count` in function factories is per-factory, not global. Each factory has its own counter starting at 1. ↓
deprecated Package uses UMD format; there is no ESM build. Importing in modern bundlers may require specific configuration. ↓
gotcha Traits are merged shallowly. Nested objects will be overwritten, not deep merged. ↓
Install
npm install test-pantry yarn add test-pantry pnpm add test-pantry Imports
- default (TestPantry) wrong
const { TestPantry } = require('test-pantry')correctimport TestPantry from 'test-pantry' - new TestPantry() wrong
const pantry = TestPantry()correctconst pantry = new TestPantry() - pantry.recipeFor() wrong
pantry.recipeFor('user', { name: 'Alice' })correctpantry.recipeFor('user', { name: 'Alice' })
Quickstart
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 } ]