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

@cats-cradle/faker-factory

v3.1.0

Published

FakerFactory creates fakes of classes using class-validator decorators for testing purposes.

Downloads

46

Readme

@cats-cradle/faker-factory

FakerFactory creates fakes of classes using class-validator decorators for testing purposes.

It creates a faker object by first inferring each property's type based the class-validator decorator. Next, it changes the object to a schema then it generates fake data for each property. Lastly, the faked object is instantiated and returned.

Getting Started

Create a fake by calling the FakerFactory.

import { FakerFactory } from '@cats-cradle/faker-factory';
import { Person } from './person.ts';

const fakerPerson = await FakerFactory.create<Person>(Person, { passed: true });
// person.ts or any class that uses class-validator decorators
import {
  IsString,
  IsInt,
  IsBoolean,
  IsUUID,
  IsCurrency,
  Min,
  ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';

export class Person {
  @IsUuid();
  public id: string;

  @IsString()
  @Length(0,50)
  @IsOptional()
  public firstName: string;

  @IsCurrency()
  public currency: string;

  @IsInt()
  @Min(0)
  public visits: number;

  @IsBoolean()
  public passed: boolean;

  @IsOptional()
  @ValidateNested({ each: true })
  @Type(() => Person)
  public guests: Person[];
}
// result
// console.log(fakerPerson);
{
  "id": "4cb85e06-1060-4bed-8224-14ec39e0dfa9",
  "firstName": "irure in",
  "currency": "887.56",
  "visits": 24235,
  "passed": true
}

Settings

FakerFactory accepts a third optional parameter for settings. Settings are used to change the way the FakerFactory works. Adjusting the settings can be useful for when writing unit tests on optional fields.

| Property | Description | Possible Values | | ----------- | -------------------------------------- | ---------------------- | | probability | probability for faking optional fields | 0, ... 0.5, ... 1 | | optionals | whether to fake optional fields | true, false, undefined |

const Person = await FakerFactory.create<Person>(
  Person,
  {},
  { optionals: true },
);

Opinions

In typescript object data can be automatically faked based on either: the property name, property typehint, separate schema, or the decorator schema.

Property names should remain somewhat consistent and fakes can be generated based on them, this is how Intermock works. However, it is unreasonable to maintain such a many-to-many relationship for faking purposes.

Generating values based on property typehinting is less then ideal currently in typescript due to compiled code being type agnostic and the special tsc requirement to add such a feature.

Generating fakes purely based on separate schema such as AJV, JIO, or validatorjs works but it is less readable than a purely decorator based approach.

A class-validator decorators and schema base approach was chosen for the following reasons:

  • Decorators are already present when using class-validation.
  • Faker data should not just be fake, it should be valid and meet class-validator requirements.
  • If a class-validator requires a string length faker data should meet that requirement.
  • It is easy to create new class-validator decorators and extend functionality.

References