1
1
# factory.ts
2
2
3
+ [ ![ willryan] ( https://circleci.com/gh/willryan/factory.ts.svg?style=svg )] ( https://app.circleci.com/pipelines/github/willryan/factory.ts )
4
+
3
5
A library to ease creation of factories for test data for Typescript
4
6
5
7
Given an interface or type definition, create a factory for generating test data. Values for each key may be defaulted or be calculated each time based on a sequence number and the values for other keys.
6
8
7
- Version 0.3.2 introduces a new set of async factory methods for cases where asynchronicity is required to generate values. See sync.spec.ts for examples.
8
-
9
- Version 0.3.3 introduces a pipeline mechanism for building up a key-value set of data. See pipeline.spec.ts for an example.
10
-
11
9
## Installation
12
10
13
11
Install using yarn:
@@ -42,11 +40,11 @@ interface Person {
42
40
import * as Factory from " factory.ts" ;
43
41
44
42
const personFactory = Factory .Sync .makeFactory <Person >({
45
- id: Factory .each (i => i ),
43
+ id: Factory .each (( i ) => i ),
46
44
firstName: " Bob" ,
47
45
lastName: " Smith" ,
48
46
fullName: " Robert J. Smith, Jr." ,
49
- age: Factory .each (i => 20 + (i % 10 ))
47
+ age: Factory .each (( i ) => 20 + (i % 10 )),
50
48
});
51
49
```
52
50
@@ -57,7 +55,7 @@ You can call `personFactory.build` with a subset of field data (`Partial<Person>
57
55
``` typescript
58
56
const james = personFactory .build ({
59
57
firstName: " James" ,
60
- fullName: " James Smith"
58
+ fullName: " James Smith" ,
61
59
});
62
60
// { id: 1, firstName: 'James', lastName: 'Smith', fullName: 'James Smith', age: 21 };
63
61
@@ -89,7 +87,7 @@ Occasionally you may want to extend an existing factory with some changes. For e
89
87
90
88
``` typescript
91
89
const anyAgeFactory = personFactory .extend ({
92
- age: Factory .each (() => randomAge (0 , 100 )) // randomAge(min:number, max:number) => number
90
+ age: Factory .each (() => randomAge (0 , 100 )), // randomAge(min:number, max:number) => number
93
91
});
94
92
95
93
anyAgeFactory .build (); // { id: 1, ..., age: <random value> };
@@ -110,7 +108,7 @@ const autoFullNameFactory = personFactory.withDerivation2(
110
108
111
109
const jamesBond = autoFullNameFactory .build ({
112
110
firstName: " James" ,
113
- lastName: " Bond"
111
+ lastName: " Bond" ,
114
112
});
115
113
// { id: 1, firstName: 'James', lastName: 'Bond', fullName: 'Bond, James Bond', age: 21 };
116
114
```
@@ -124,7 +122,7 @@ Alternatively, if you need to read more than 5 properties, or just don't want to
124
122
``` typescript
125
123
const autoFullNameFactory = personFactory .withDerivation (
126
124
" fullName" ,
127
- person => ` ${person .lName }, ${person .fName } ${person .lName } `
125
+ ( person ) => ` ${person .lName }, ${person .fName } ${person .lName } `
128
126
);
129
127
```
130
128
@@ -141,10 +139,10 @@ Sometimes you have two factories you want to combine into one. So essentially yo
141
139
``` typescript
142
140
const timeStamps = Sync .makeFactory ({
143
141
createdAt: Sync .each (() => new Date ()),
144
- updatedAt: Sync .each (() => new Date ())
142
+ updatedAt: Sync .each (() => new Date ()),
145
143
});
146
144
const softDelete = Sync .makeFactory ({
147
- isDeleted: false
145
+ isDeleted: false ,
148
146
});
149
147
interface Post {
150
148
content: string ;
@@ -153,7 +151,7 @@ interface Post {
153
151
isDeleted: boolean ;
154
152
}
155
153
const postFactory: Sync .Factory <Post > = makeFactory ({
156
- content: " lorem ipsum"
154
+ content: " lorem ipsum" ,
157
155
})
158
156
.combine (timeStamps )
159
157
.combine (softDelete );
@@ -179,11 +177,11 @@ interface Person {
179
177
180
178
const personFactory = Factory .Sync .makeFactoryWithRequired <Person , " parent_id" >(
181
179
{
182
- id: Factory .each (i => i ),
180
+ id: Factory .each (( i ) => i ),
183
181
firstName: " Bob" ,
184
182
lastName: " Smith" ,
185
183
fullName: " Robert J. Smith, Jr." ,
186
- age: Factory .each (i => 20 + (i % 10 ))
184
+ age: Factory .each (( i ) => 20 + (i % 10 )),
187
185
}
188
186
);
189
187
0 commit comments