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

factory-node

v2.0.0

Published

FactoryBot-like factories to JavaScript/TypeScript

Downloads

78

Readme

factory-node

factory-node is a flexible library for DRY in unit and integration tests. Instead of defining sample (often hard-coded) data structures in every test, you work with the common mirror of your application models.

The library is heavily inspired by Ruby's ecosystem: factory_bot, rspec, faker.

It implements TypeScript types safety for your factories in the first place. Partly, it explains the choice of DSL. The factories, attributes and traits are immutable, meaning that you need to always use the returned object. There are no in-place mutations because Typescript does not allow to mutate type for an instance.

Installation

Install using yarn:

yarn add -D factory-node

Install using npm:

npm install --save-dev factory-node

Example

Generic example

import { faker } from '@faker-js/faker'
import { holding } from 'factory-node'

const factory = holding()
  .define('user', (f) => (
    f.attribute('id', () => faker.string.uuid())
     .attribute('username', () => faker.internet.userName())
     .attribute('email', () => faker.internet.email())
     .attribute('password', () => faker.internet.password())
  ))
  .define('post', (f) => (
    f.attribute('userId', () => faker.string.uuid())
     .attribute('title', () => faker.lorem.sentence())
     .attribute('body', () => faker.lorem.paragraphs())
  ))

const post = await factory.build('post')

Features

Traits

Traits allow you to create variations of your factory without duplicating code. They are particularly useful when you need to create objects with specific characteristics or states.

Traits don't change the original factory definition, so you can't change attributes return types or add new attributes.

You can define traits using the trait method on your factory. Each trait is a function that modifies the factory's attributes.

import { holding } from 'factory-node'

const factory = holding().define('user', (f) => (
  f.attribute('name', () => 'Default Name')
   .attribute('age', () => 30)
   .trait('senior', (f) => f.attribute('age', () => 65))
   .trait('male', (f) => f.attribute('name', () => 'John'))
))

Once defined, you can use traits when building objects:

// Using a single trait
const seniorUser = await userFactory.build('user', 'senior')
// Result: { name: 'Default Name', age: 65 }

// Using multiple traits
const seniorMaleUser = await userFactory.build('user', 'senior', 'male')
// Result: { name: 'John', age: 65 }

// Combining traits with overrides
const customSeniorUser = await userFactory.build('user', 'senior', { name: 'Alice' })
// Result: { name: 'Alice', age: 65 }

initializeWith

The initializeWith method allows you to customize the final output of your factory. This is particularly useful when you want to return instances of specific classes instead of plain JavaScript objects.

Here's an example of how to use initializeWith with a sample User model:

import { holding } from 'factory-node'

class User {
  private name: string;
  private age: number;

  constructor(props: { name: string; age: number }) {
    this.name = props.name;
    this.age = props.age;
  }

  greet() {
    return `Hello, I'm ${this.name} and I'm ${this.age} years old.`;
  }
}

const factory = holding().define('user', (f) =>
  f.attribute('name', () => 'John Doe')
   .attribute('age', () => 30)
   .initializeWith((attrs) => new User(attrs))
);

const user = await holding.build('user');
console.log(user instanceof User); // true
console.log(user.greet()); // "Hello, I'm John Doe and I'm 30 years old."

// You can still override attributes
const youngUser = await holding.build('user', { age: 18 });
console.log(youngUser.greet()); // "Hello, I'm John Doe and I'm 18 years old."

Future plans

  • [x] traits
  • [x] initialize_with function
  • [ ] associations
  • [ ] custom operations (integration with your ORM)
  • [ ] async attributes
  • [ ] definition files