0.0.3 • Published 2 years ago

ts-fixture-builder v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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