https://github.com/tudddorrr/hefty
Easy, unopinionated and intuitive Typescript fixtures
https://github.com/tudddorrr/hefty
factory fixtures mock seeding testing
Last synced: 4 months ago
JSON representation
Easy, unopinionated and intuitive Typescript fixtures
- Host: GitHub
- URL: https://github.com/tudddorrr/hefty
- Owner: tudddorrr
- Created: 2021-02-25T23:37:43.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-02T14:56:51.000Z (almost 2 years ago)
- Last Synced: 2025-10-27T00:49:08.411Z (8 months ago)
- Topics: factory, fixtures, mock, seeding, testing
- Language: TypeScript
- Homepage: https://npmjs.com/package/hefty
- Size: 212 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hefty
Easy, unopinionated and intuitive Typescript fixtures.
## Installation
`npm install hefty --save-dev`
## Usage
Hefty lets you create factories, chain multiple states and override those states too. Check out the tests for more examples.
### Create a Factory
Factories are made up of entities with one or more states applied to them. States are called with the same params you get with `Array.map()`.
```
// User.ts
class User {
email: string
emailConfirmed: boolean
onboarded: boolean
}
// UserFactory.ts
import { Factory } from 'hefty'
class UserFactory extends Factory {
constructor() {
super(User)
}
hasConfirmed(): this {
return this.state(() => ({
emailConfirmed: true
}))
}
hasOnboarded(): this {
return this.state(() => ({
onboarded: true
}))
}
}
```
### Create some users
```
const factory = new UserFactory()
const user1: User = await factory.one()
// -> emailConfirmed = false
const user2: User = await factory.emailConfirmed().one()
// -> emailConfirmed = true
const user3: User = await factory.emailConfirmed().onboarded().one()
// -> emailConfirmed = true, onboarded = true
const user4: User = await factory.emailConfirmed().state(() => ({ email: hello@web.site })).one()
// -> emailConfirmed = true, email = hello@web.site
const users: User[] = await factory.emailConfirmed().many(3)
// -> generates 3 users with emailConfirmed = true
```
State functions defined in the factory and `state()` can be chained as many times as you like in any order. They'll all be applied sequentially when `one()` or `many()` is called.
### Factories with default states
Factories can implement a `definition()` function that is called before any other states are applied.
```
export default class UserFactory extends Factory {
constructor() {
super(User)
}
protected definition(): void {
this.state(() => {
createdAt: 'today',
emailConfirmed: true
})
}
onboarded(): this {
return this.state(() => ({
onboarded: true
}))
}
emailConfirmed(): this {
return this.state(() => ({
emailConfirmed: true
}))
}
}
const factory = await new UserFactory().one()
// => createdAt = today, emailConfirmed: true
```
### Promises
Hefty will automatically resolve any promise-based `state()` callbacks.
```
export default class UserFactory extends Factory {
constructor() {
super(User)
}
protected definition(): void {
this.state(async () => ({
password: await bcrypt.hash('password', 10)
}))
}
}
```
### Constructors
You can pass constructor params to the `construct()` function. Entities will be initialised with these params before the definition is applied.
```
// User.ts
class User {
email: string
constructor(email: string) {
this.email = email
}
}
// User.test.ts
const email = 'hello@mail.com'
expect((await new UserFactory().construct(email).one()).email).toBe(email)
```