ts-fixture-builder
v0.0.3
Published
fixture generator based on 'builder' pattern
Downloads
4
Maintainers
Readme
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: '[email protected]' })
}
public static defaultAll() {
return new InjectionBuilder<User>(new User())
.with({ name: 'John' })
.with({ email: '[email protected]' })
.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