File tree Expand file tree Collapse file tree 1 file changed +36
-17
lines changed Expand file tree Collapse file tree 1 file changed +36
-17
lines changed Original file line number Diff line number Diff line change @@ -89,12 +89,13 @@ The factory generates strictly typed mock data.
89
89
``` ts
90
90
// simple
91
91
const book0 = await BookFactory .build ();
92
- expect (book0 ).toStrictEqual ({
93
- __typename: ' Book' ,
94
- id: ' Book-0' ,
95
- title: ' apple' ,
96
- author: undefined ,
97
- });
92
+ console .log (book0 );
93
+ // {
94
+ // __typename: 'Book',
95
+ // id: 'Book-0',
96
+ // title: 'apple',
97
+ // author: undefined,
98
+ // }
98
99
assertType <{
99
100
__typename: ' Book' ;
100
101
id: string ;
@@ -106,17 +107,18 @@ assertType<{
106
107
const book1 = await BookFactory .build ({
107
108
author: await AuthorFactory .build (),
108
109
});
109
- expect (book1 ).toStrictEqual ({
110
- __typename: ' Book' ,
111
- id: ' Book-1' ,
112
- title: ' orange' ,
113
- author: {
114
- __typename: ' Author' ,
115
- id: ' Author-0' ,
116
- name: ' Tom' ,
117
- books: undefined ,
118
- },
119
- });
110
+ console .log (book0 );
111
+ // {
112
+ // __typename: 'Book',
113
+ // id: 'Book-1',
114
+ // title: 'orange',
115
+ // author: {
116
+ // __typename: 'Author',
117
+ // id: 'Author-0',
118
+ // name: 'Tom',
119
+ // books: undefined,
120
+ // },
121
+ // }
120
122
assertType <{
121
123
__typename: ' Book' ;
122
124
id: string ;
@@ -134,6 +136,23 @@ assertType<{
134
136
135
137
The library has several notable features. And many of them are inspired by [ FactoryBot] ( https://thoughtbot.github.io/factory_bot/ ) .
136
138
139
+ ### Dynamic Fields
140
+
141
+ The ` dynamic ` function allows you to define fields with a dynamic value.
142
+
143
+ ``` ts
144
+ const BookFactory = defineBookFactory ({
145
+ defaultFields: {
146
+ id: dynamic (() => faker .datatype .uuid ()),
147
+ title: ' Yuyushiki' ,
148
+ },
149
+ });
150
+ expect (await BookFactory .build ()).toStrictEqual ({
151
+ id: expect .any (String ), // Randomly generated UUID
152
+ title: ' Yuyushiki' ,
153
+ });
154
+ ```
155
+
137
156
### Sequences
138
157
139
158
Sequences allow you to build sequentially numbered data.
You can’t perform that action at this time.
0 commit comments