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

factoryse

v2.0.2

Published

TypeScript-based test factory library that simplifies the creation of test data for your Node.js applications.

Downloads

52

Readme

Factoryse

build semantic-release: npm node.js: >=16.20.2

Factoryse is a test factory library built with TypeScript, designed to streamline the creation of test data for your Node.js applications. Craft and manage test objects effortlessly, optimising the efficiency of your testing workflow. This project draws inspiration from Ruby's renowned Factory Bot, bringing similar ease and consistency to the world of TypeScript and Node.js testing.

Factoryse is designed to streamline the creation of test data, offering a simple and efficient approach to generate random (or deterministic, if preferred) data, thereby reducing redundancy in your testing setup.

Factoryse is compatible with any modern testing suite, including Vitest and Jest, without being tied to any specific one.

Install

npm install --save-dev factoryse

How to use

Defining a factory

First, define your factory structure. This involves specifying the data model and creating the factory instance.

// Define the User shape
interface User {
  email: string;
  createdAt?: Date;
}

// Create the user factory
const user = new Factory<User>(faker => ({
  email: faker.internet.email()
}));

It's recommended to use faker during factory instantiation for generating diverse random data.

Generating entries

Generate a single entry:

const factory = user.make();
// Generates: [{ email: "[email protected]" }]

To generate multiple entries:

const factory = user.make(2);
// Generates: [{ email: "[email protected]" }, { email: "[email protected]" }]

Retrieving entries

Leverage the get() action to access entries created by your Factory. Factoryse offers a variety of query methods to facilitate this retrieval process.

// Retrieve all entries
users.get().all(); // User[]

// Retrieve the first entry or the first (`n: number`) entries
users.get().first(); // User[]
users.get().first(n); // User[]

// Retrieve the last entry or the last (`n: number`) entries
users.get().last(); // User[]
users.get().last(n); // User[]

// Retrieve a random entry or multiple random (`n: number`) entries
users.get().any(); // User[]
users.get().any(n); // User[]

// Retrieve a single entry by its index (`n: number`)
users.get().at(n); // User

// Retrieve multiple entries by their index (`n: number[]`)
users.get().pick(n); // User[]

// Retrieve multiple entries between indexes (`start: number, end: number`)
users.get().between(start, end); // User[]

// Retrieve multiple entries by criteria (`c: Partial<User>`)
users.get().by(c); // User[]

Editing entries

Leverage the set(source) action to edit entries in your Factory. Factoryse offers a variety of query methods to facilitate this editing process. Each method used for editing entries will return the modified entries. To retrieve the complete set of entries from the factory after making edits, refer to the get() action detailed in the Retrieving Entries section.

// Closure to apply modifications to entries
const source = faker => ({ createdAt: faker.date.recent() })

// Update all entries
users.set(source).all(); // User[]

// Update the first entry or the first (`n: number`) entries
users.set(source).first(); // User[]
users.set(source).first(n); // User[]

// Update the last entry or the last (`n: number`) entries
users.set(source).last(); // User[]
users.set(source).last(n); // User[]

// Update a random entry or multiple random (`n: number`) entries
users.set(source).any(); // User[]
users.set(source).any(n); // User[]

// Update a single entry by its index (`n: number`)
users.set(source).at(n); // User

// Update multiple entries by their index (`n: number[]`)
users.set(source).pick(n); // User[]

// Update multiple entries between indexes (`start: number, end: number`)
users.set(source).between(start, end); // User[]

// Update multiple entries by criteria (`c: Partial<User>`)
users.set(source).by(c); // User[]

Deleting entries

Leverage the delete() action to delete entries from your Factory. Factoryse offers a variety of query methods to facilitate this deleting process. Each method used for deleting entries will return the deleted entries. To retrieve the complete set of entries from the factory after making deletions, refer to the get() action detailed in the Retrieving Entries section.

// Delete all entries
users.delete().all(); // User[]

// Delete the first entry or the first (`n: number`) entries
users.delete().first(); // User[]
users.delete().first(n); // User[]

// Delete the last entry or the last (`n: number`) entries
users.delete().last(); // User[]
users.delete().last(n); // User[]

// Delete a random entry or multiple random (`n: number`) entries
users.delete().any(); // User[]
users.delete().any(n); // User[]

// Delete a single entry by its index (`n: number`)
users.delete().at(n); // User

// Delete multiple entries by their index (`n: number[]`)
users.delete().pick(n); // User[]

// Delete multiple entries between indexes (`start: number, end: number`)
users.delete().between(start, end); // User[]

// Delete multiple entries by criteria (`c: Partial<User>`)
users.delete().by(c); // User[]