Skip to content

derbent-ninjas/ts-fixture-builder

Repository files navigation

TS FIXTURE BUILDER


WHAT IS THIS?

This is convenient fixture generator for tests based on builder pattern.


Install

npm i ts-fixture-builder

Example:


  • Let's say you have a User entity and you want it to be generated for tests:
class User {
  name: string;
  email: string;
  lastName?: string;
  age?: number;
}

  • Then you can declare builder of this user:
import { InjectionBuilder } from 'ts-fixture-builder';

class UserBuilder {
  public static defaultOnlyRequired() {
    return new InjectionBuilder<User>(new User())
      .with({ name: 'John' })
      .with({ email: 'john@gmail.com' })
  }

  public static defaultAll() {
    return new InjectionBuilder<User>(new User())
      .with({ name: 'John' })
      .with({ email: 'john@gmail.com' })
      .with({ lastName: 'Smith' })
      .with({ age: 20 })
  }
}

  • finally, you can use this builder in your tests:
const teenagerFixture = UserBuilder.defaultOnlyRequired()
  .with({ age: 16 })
  .result

const adultFixture = UserBuilder.defaultOnlyRequired()
  .with({ age: 30 })
  .result

LICENCE


This library is MIT licensed