npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cooky-cutter

v1.5.4

Published

Object factories for testing in TypeScript

Downloads

21,937

Readme

🍪 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 }