🍪 cooky-cutter
Simple, type safe* object factories for JavaScript tests. (*with TypeScript)
![npm type definitions](https://img.shields.io/npm/types/cooky-cutter.svg)
Read the Release Annoucement.
The problem
You need to write maintainable tests for JavaScript. The code depends on
specific entity types defined in the data model. These entities might initially
get stubbed out in tests (Mocha, Jest, etc) as plain objects. As complexity
grows, you move to factory functions (or another package that does this) to
avoid the duplication. A new column gets added, an old one gets removed or maybe
an entirely new entity is added. The breaking change isn't noticed until the
entire test suite runs (or maybe never).
The solution
cooky-cutter
is a light package
that leverages TypeScript to define and create factories. Simply pass the type
as a generic (assuming you already have a type or interface defined for each
entity type). Whenever the entity type changes, the factories become invalid!
Installation
npm install --save-dev cooky-cutter
yarn add --dev cooky-cutter
Basic Usage
For more documentation and examples, read the full documentation.
import { define, random, sequence } from "cooky-cutter";
interface User {
id: number;
firstName: string;
lastName: string;
age: number;
}
const user = define<User>({
id: random,
firstName: i => `Bob #${i}`,
lastName: "Smith",
age: sequence
});
console.log(user());
console.log(user());
console.log(user());