Skip to content

Commit 4fb084c

Browse files
committed
add description for dynamic fields
1 parent f0584ee commit 4fb084c

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

README.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ The factory generates strictly typed mock data.
8989
```ts
9090
// simple
9191
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+
// }
9899
assertType<{
99100
__typename: 'Book';
100101
id: string;
@@ -106,17 +107,18 @@ assertType<{
106107
const book1 = await BookFactory.build({
107108
author: await AuthorFactory.build(),
108109
});
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+
// }
120122
assertType<{
121123
__typename: 'Book';
122124
id: string;
@@ -134,6 +136,23 @@ assertType<{
134136

135137
The library has several notable features. And many of them are inspired by [FactoryBot](https://thoughtbot.github.io/factory_bot/).
136138

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+
137156
### Sequences
138157

139158
Sequences allow you to build sequentially numbered data.

0 commit comments

Comments
 (0)