Skip to content
πŸͺ Object factories for testing in TypeScript
TypeScript JavaScript
Branch: master
Clone or download
dependabot and skovy Bump lodash from 4.17.11 to 4.17.14 (#20)
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.11 to 4.17.14.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](lodash/lodash@4.17.11...4.17.14)

Signed-off-by: dependabot[bot] <support@github.com>
Latest commit 4d798b7 Jul 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
docs
src
.gitignore
.npmignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
jest.config.js
package-lock.json
package.json
tsconfig.json

README.md

πŸͺ cooky-cutter

Simple, type safe* object factories for JavaScript tests. (*with TypeScript)

Travis branch Coverage Status npm npm type definitions

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
# or
yarn add --dev cooky-cutter

Basic Usage

For more documentation and examples, read the full documentation.

import { define, random, sequence } from "cooky-cutter";

// Define an interface (or type) for the entity
interface User {
  id: number;
  firstName: string;
  lastName: string;
  age: number;
}

// Define a factory that represents the defined interface
const user = define<User>({
  id: random,
  firstName: i => `Bob #${i}`,
  lastName: "Smith",
  age: sequence
});

// Invoke the factory a few times
console.log(user());
// => { id: 980711200, firstName: 'Bob #1', lastName: 'Smith', age: 1 }

console.log(user());
// => { id: 1345667839, firstName: 'Bob #2', lastName: 'Smith', age: 2 }

console.log(user());
// => { id: 796816401, firstName: 'Bob #3', lastName: 'Smith', age: 3 }
You can’t perform that action at this time.