ts-fixture-builder
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

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

Package Sidebar

Install

npm i ts-fixture-builder

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

5.73 kB

Total Files

12

Last publish

Collaborators

  • bota