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.io

v0.1.4

Published

Modern class based mock data generation with typescript support.

Downloads

949

Readme

Test Codecov License Downloads Version

factory.io

A modern class-based test factory generation with TypeScript support. It integrates exceptionally well with popular TypeScript class-based libraries such as TypeORM.

Table of contents

Usage

Factories can be constructed with the following (chaining) methods.

  • ctor (constructor arguments)
  • props or prop (properties to be assigned)
  • computed (values calculated based on props or default object properties)
  • mixins (factories expanded by current factory)
  • build (returns class factory)

Things to remember

  • Methods can be called in any order - execution order is predefined
  • Execution order:
    • constructor
    • removeUnassignedProperties (if set in options)
    • mixins
    • props
    • computed
    • partial
  • Relations between objects can be assigned with computed method.

Ctor

Ctor arguments are passed to class constructor during object initialization. This is useful when a class cannot be initialized without certain constructor arguments.

  • Plain values are always the same.
  • Functions are recalculated each time an object is built.
const userFactory = FactoryBuilder.of(User)
  .ctor([faker.random.number, 12])
  .build();

const result = userFactory.buildOne();

Props

Props should be provided as values, functions or nested objects consisting of values and functions.

  • Plain values are always the same.
  • Functions are recalculated each time an object is built.
const userFactory = FactoryBuilder.of(User)
  .props({
    age: faker.random.number,
    username: faker.internet.userName,
    friend: {
      username: faker.internet.userName,
    },
  })
  .build();

const result = userFactory.buildOne();

Computed

Computed properties should be provided as functions or nested objects. They can reference the main object (and objects it references) after mixins and props are assigned.

const userFactory = FactoryBuilder.of(User)
  .computed({
    age: (e) => e.age * 2,
  })
  .props({
    age,
  })
  .build();

const result = userFactory.buildOne();

Mixins

Use mixins to extend previously constructed factories. Remember that mixins are resolved in provided order and before props and computed of factory currently being expanded.

const mixinUserFactory = FactoryBuilder.of(User)
  .props({
    age: faker.random.number,
    username: faker.internet.userName,
  })
  .build();

const userFactory = FactoryBuilder.of(User)
  .props({
    /*
      Mixin age value is overridden
    */
    age: faker.random.number,
  })
  .mixins([mixinUserFactory])
  .build();

const result = userFactory.buildOne();

Options

  • sequenceField - Object property to which sequence value should be assigned
  • sequenceTransformer - Custom function responsible for sequence assignment (allows to modify the value pre-assignment)
  • removeUnassignedProperties - Whether undefined properties should be removed (as the constructor is passed with no arguments, fields without default values are assigned undefined)
  • defaultSequenceValue - Initial sequence value, incremented by one each time an object is build

Build

build() method transforms FactoryBuilder into Factory. This process cannot be reversed. Factories cannot be assigned new properties.

Factory

Factory object has the following methods.

  • buildOne
  • buildMany
  • resetSequence

Examples

Classes

const userFactory = FactoryBuilder.of(User)
  .props({
    age: faker.random.number,
    username: faker.internet.userName,
  })
  .build();

const result = userFactory.buildOne({ id: 1 });
const userFactory = FactoryBuilder.of(User)
  .props({
    age: faker.random.number,
    username: faker.internet.userName,
  })
  .build();

const result = userFactory.buildOne({ id: 1 });

Interfaces

const userFactory = FactoryBuilder<IUser>.of()
  .props({ age: faker.random.number, username: faker.internet.userName })
  .computed({
    monthsAlive: (user) => user.age * 12,
  })
  .build();

const result = userFactory.buildMany(4);
const userFactory = FactoryBuilder<IUser>.of()
  .props({ age: faker.random.number, username: faker.internet.userName })
  .build();

const result = userFactory.buildMany(5);

TypeORM integration

Entity

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true })
  username: string;

  @Column()
  email: string;
}

Factory

export const userFactory = FactoryBuilder.of(User)
  .options({ sequenceField: 'id' })
  .props({
    email: faker.internet.email,
    password: faker.internet.password,
  })
  .build();

Utils

async function saveOne(current: any) {
  try {
    const repository = getConnection().getRepository(current.constructor.name);
    return await repository.save(current);
  } catch (e) {
    console.log(e);
  }
}

Usage

it('should save data to db', async () => {
  const user = await saveOne(userFactory.buildOne());

  const result = await getConnection()
    .getRepository(user.constructor.name)
    .findOne({ id: user.id });

  expect(result).toEqual(user);
});

License

This project is licensed under the MIT License - see the LICENSE.md file for details.