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 🙏

© 2026 – Pkg Stats / Ryan Hefner

test-data-factory

v0.2.0

Published

Flexible test-data factories and store mechanisms.

Readme

🚧 This package is under heavy development. Documentation is currently lacking and will be added later. Feel free to explore, adventurer! 🚧

Installation

You can install Test Data Factory with this shell command:

npm i -D test-data-factory

Usage Guide

Test Data Factory is based on two main concepts:

  • Factories for defining and creating test-data.
  • Stores as a pluggable test-data storage mechanism that is seeded by factories.

Stores are a powerful but completely optional concept. They interface well with factories, but factories are designed to be used standalone. Even if you do not end up using stores, Test Data Factory can still provide you a powerful and flexible Factory API for defining and creating test-data.

Factories

import { Factory } from "test-data-factory";

interface Task {
  id: number;
  name: string;
  status: "open" | "completed";
}

class TaskFactory extends Factory<Task> {
  protected override construct(): Task {
    return {
      id: this.ctx.sequence,
      name: `Task ${this.ctx.sequence}`,
      status: "open",
    };
  }

  get completed() {
    return this.refine({ status: "completed" });
  }
}
export const taskFactory = TaskFactory.create();

// Build a task without any overrides
const task = taskFactory.build();
// Provide params to override the result
const task = taskFactory.build({ name: "Check Mailbox" });
// Builds a completed task
const task = taskFactory.completed.build();

Factory Shorthand

import { defineFactory } from "test-data-factory";

const factory = defineFactory<string>((ctx) => `T-${ctx.sequence}`);

// Shorthand factories can be used like "normal factories"
const id = factory.build();
const ids = factory.buildMany(3);

Stores

import { Store } from "test-data-factory";
import { taskFactory } from "./task.js";
import { userFactory } from "./user.js";

export class DataStore extends Store {
  tasks = this.collection(taskFactory);
  users = this.collection(userFactory);

  // Optionally, you can seed your stores with initial data.
  // Called when an instance is created.
  protected override async initialize(): Promise<void> {
    await userFactory.seed(this);
  }
}

// Creating and using the Store...
const store = await DataStore.create();
const tasks = await taskFactory.seedMany(store, 3);
await userFactory.seed(store, { name: "Christoph" });

const task = store.tasks.find({ where: (t) => t.id === 3 });
const latestUser = store.users.latest();

// A "dump" is an object that contains the all store entries.
// The dump is fully type-safe, in case you want to operate on it further.
console.log(store.dump());

// Removes all entries and re-initializes the store.
await store.reset();

Advanced Usage

Extend the Factory Context

You can create your own base factory that adds additional functionality for all factories. Importantly, you can extend the FactoryContext that is available in the construct method of both 'class-based' and shorthand factories.

For example, you could add an instance of Faker to the context, which can ensure that all factories use the same locale.

import { Factory, type ConstructFn, type Params } from "test-data-factory";
import { faker } from "@faker-js/faker/locale/de";

export abstract class BaseFactory<Shape> extends Factory<Shape> {
  protected readonly faker = faker;

  protected override createContext(params?: Params<Shape>) {
    const ctx = super.createContext(params);
    return {
      ...ctx,
      faker: this.faker,
    };
  }
}

// Create your own shorthand Factory that uses your custom context.
export function defineFactory<Shape>(
  construct: ConstructFn<Shape, BaseFactory<Shape>>,
): BaseFactory<Shape> {
  class InlineFactory extends BaseFactory<Shape> {
    protected override construct(): Shape {
      return construct(this.ctx);
    }
  }
  return InlineFactory.create();
}

License

This package is published under the MIT license.