• Stars
    star
    133
  • Rank 272,600 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸͺ Object factories for testing in TypeScript

πŸͺ 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 }

More Repositories

1

typed-scss-modules

🎁 Generate type definitions (.d.ts) for CSS Modules using SCSS
TypeScript
626
star
2

llm-markdown

Demo rendering rich responses from LLMs
TypeScript
112
star
3

react-hooks-typescript-example

🎣 Examples of using React Hooks with TypeScript
TypeScript
80
star
4

font-awesome-codemod

βš™οΈ Font Awesome codemod script
TypeScript
49
star
5

react-suspense-img

πŸ–Ό A simple React image component that suspends while loading
TypeScript
19
star
6

graphql-blog-client

πŸš€ React Apollo client for a GraphQL blog.
TypeScript
14
star
7

css-codemod

🐍 Toolkit for running codemods over CSS files
TypeScript
13
star
8

sudoku-reasonml

πŸ“ A simple Sudoku board implemented with ReasonML React
OCaml
7
star
9

react-concurrent-mode-typescript-example

πŸ”€ Examples of using React Concurrent Mode with TypeScript
HTML
6
star
10

graphql-blog-api

πŸ’Ž Rails 5 API for a GraphQL blog and client agnostic
Ruby
4
star
11

skovy.dev

✍️My personal site, blog, etc
TypeScript
4
star
12

rails-phlex-i18n-vscode

VSCode Extension to Extract Strings in Rails Phlex Views to Translation Files
TypeScript
4
star
13

plotlyjs-rails

plotlyjs-rails wraps the Plotly.js Library for use with rails
Ruby
3
star
14

webpack-tree-shaking-demo

Example of tree shaking Font Awesome in a simple webpack app
JavaScript
2
star
15

typescript-codemod-examples

Examples of code audits and code transforms (codemods) written in TypeScript
TypeScript
2
star
16

yarn-check-webpack-plugin

πŸ“¦ Validate the proper packages are installed
TypeScript
2
star
17

react-server-side-rendering

Examples of Server Side Rendering (SSR) React
JavaScript
1
star
18

nearest-neighbor-classifier

Basic k-nearest neighbors classifier
Python
1
star
19

nextjs-stitches-capsize

Next.js app styled with Stitches, and typography trimmed with Capsize
TypeScript
1
star
20

neural-network

A Simple Artificial Neural Network
Python
1
star
21

pca-decomposition

Principal component analysis (PCA) Decomposition of track audio summary
Python
1
star
22

tensorflow-dice-model

🎲 Tensorflow Lite models for detecting and classifying dice
Python
1
star
23

css-codemod-demo

Example of a custom CSS codemod built with PostCSS
TypeScript
1
star
24

song-clustering

Basic clustering using song data from Echonest
Python
1
star