Gathering detailed insights and metrics for @reflow-work/test-fixture-factory
Gathering detailed insights and metrics for @reflow-work/test-fixture-factory
Gathering detailed insights and metrics for @reflow-work/test-fixture-factory
Gathering detailed insights and metrics for @reflow-work/test-fixture-factory
npm install @reflow-work/test-fixture-factory
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1 Stars
47 Commits
3 Branches
1 Contributors
Updated on 17 Nov 2023
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
23.4%
179
Compared to previous day
Last week
10.6%
627
Compared to previous week
Last month
108.2%
2,428
Compared to previous month
Last year
730.6%
13,339
Compared to previous year
1
Create test fixtures easily!
This library is inspired by Ecto Test factories.
npm install --save-dev @reflow-work/test-fixture-factory
1import { FixtureFactory } from '@reflow-work/test-fixture-factory'; 2 3type UserType = { 4 name: string; 5 age: number; 6}; 7 8const userFactory = new FixtureFactory<UserType>(() => { 9 return { name: 'name', age: 30 }; 10}); 11 12// creating a user 13const user0 = userFactory.create(); 14 15// { name: 'name', age: 30 } 16 17// creating a user with overriding attributes 18const user1 = userFactory.create({ name: 'new name' }); 19 20// { name: 'new name', age: 30 } 21 22// creating users 23const [user2, user3] = userFactory.createList(2); 24 25// [{ name: 'name', age: 30 }, { name: 'name', age: 30 }] 26 27// creating users with overriding attributes 28const [user4, user5] = userFactory.createList(2, [ 29 { name: 'new name' }, 30 { age: 40 }, 31]); 32 33// [{ name: 'new name', age: 30 }, { name: 'name', age: 40 }]
You can make more complicated fixture factory with custom attributes of generator.
1import { FixtureFactory } from '@reflow-work/test-fixture-factory'; 2 3const integerFactory = new FixtureFactory< 4 number, 5 { min: number | undefined; max: number | undefined } 6>((attrs) => { 7 const minNumber = attrs?.min ?? 0; 8 const maxNumber = attrs?.max ?? Number.MAX_SAFE_INTEGER; 9 10 return Math.floor(Math.random() * (maxNumber - minNumber)) + minNumber; 11}); 12 13const number = integerFactory.create({ min: 0, max: 10 });
You can also composite those factories.
1import { FixtureFactory } from '@reflow-work/test-fixture-factory'; 2 3type UserType = { 4 name: string; 5 age: number; 6}; 7 8const integerFactory = new FixtureFactory< 9 number, 10 { min: number | undefined; max: number | undefined } 11>((attrs) => { 12 const minNumber = attrs?.min ?? 0; 13 const maxNumber = attrs?.max ?? Number.MAX_SAFE_INTEGER; 14 15 return Math.floor(Math.random() * (maxNumber - minNumber)) + minNumber; 16}); 17 18const userFactory = new FixtureFactory<UserType>(() => { 19 return { 20 name: 'name', 21 age: integerFactory.create({ min: 0, max: 100 }), 22 }; 23});
When creating a FixtureFactory
using a class, you should use | undefined = undefined
to the fields instead of ?
(optional). This is because the generator is set to return Required<T>
. This prevents merging non-existent keys when overriding attrs
.
See also When to use typescript optional property? How is it different from declaring property as undefined
1class UserClass { 2 constructor( 3 public name: string, 4 public age: number | undefined = undefined // instead of `number?` 5 ); 6} 7 8const userFactory = new FixtureFactory<UserClass>(() => ...)
For more information, see also test codes
To install dependencies:
1bun install
To run test:
1bun test src
This project was created using bun init
in bun v1.0.6. Bun is a fast all-in-one JavaScript runtime.
No vulnerabilities found.
No security vulnerabilities found.