You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `faker.derive()` call clones the instance and re-initializes the seed of the clone with a generated value from the original.
364
+
This decouples the generation of the list from generating a user.
365
+
It does not matter how many properties you add to or remove from the `User` the following rows will not change.
366
+
367
+
::: tip Note
368
+
The following is only relevant, if you want to avoid changing your generated objects as much as possible:
369
+
370
+
When adding one or more new properties, we recommend generating them last, because if you create a new property in the middle of your object, then the properties after that will still change (due to the extra seed consumption).
371
+
When removing properties, you can continue calling the old method (or a dummy method) to consume the same amount of seed values.
372
+
:::
373
+
374
+
This also works for deeply nested complex objects:
375
+
376
+
```ts
377
+
function createLegalAgreement(faker:Faker) {
378
+
const derivedFaker =faker.derive();
379
+
return {
380
+
_id: derivedFaker.string.uuid(),
381
+
partyA: createRandomUser(derivedFaker),
382
+
partyB: createRandomUser(derivedFaker),
383
+
};
384
+
}
385
+
386
+
function createRandomUser(faker:Faker):User {
387
+
const derivedFaker =faker.derive();
388
+
return {
389
+
_id: derivedFaker.string.uuid(),
390
+
firstName: derivedFaker.person.firstName(),
391
+
lastName: derivedFaker.person.lastName(),
392
+
createdDate: derivedFaker.date.past(),
393
+
address: createRandomAddress(derivedFaker),
394
+
};
395
+
}
396
+
397
+
function createRandomAddress(faker:Faker):Address {
398
+
const derivedFaker =faker.derive();
399
+
return {
400
+
_id: derivedFaker.string.uuid(),
401
+
streetName: derivedFaker.location.street(),
402
+
};
403
+
}
404
+
```
405
+
406
+
::: warning Warning
407
+
Migrating your existing data generators will still change all data once, but after that they are independent.
408
+
So we recommend writing your methods like this from the start.
409
+
:::
410
+
411
+
::: info Note
412
+
Depending on your preferences and requirements you can design the methods either like this:
413
+
414
+
```ts
415
+
function createRandomXyz(faker:Faker):Xyz {
416
+
return {
417
+
_id: faker.string.uuid(),
418
+
};
419
+
}
420
+
421
+
createRandomXyz(faker.derive());
422
+
createRandomXyz(faker.derive());
423
+
createRandomXyz(faker.derive());
424
+
```
425
+
426
+
or this
427
+
428
+
```ts
429
+
function createRandomXyz(faker:Faker):Xyz {
430
+
const derivedFaker =faker.derive();
431
+
return {
432
+
_id: derivedFaker.string.uuid(),
433
+
};
434
+
}
435
+
436
+
createRandomXyz(faker);
437
+
createRandomXyz(faker);
438
+
createRandomXyz(faker);
439
+
```
440
+
441
+
The sole difference being more or less explicit about deriving a faker instance (writing more or less code).
442
+
:::
443
+
444
+
## Create identical complex objects
445
+
446
+
If you want to create two identical objects, e.g. one to mutate and one to compare it to,
447
+
then you can use `faker.clone()` to create a faker instance with the same settings and seed as the original.
448
+
449
+
```ts
450
+
const clonedFaker =faker.clone();
451
+
const user1 =createRandomUser(faker);
452
+
const user2 =createRandomUser(clonedFaker);
453
+
expect(user1).toEqual(user2); ✅
454
+
455
+
subscribeToNewsletter(user1);
456
+
// Check that the user hasn't been modified
457
+
expect(user1).toEqual(user2); ✅
458
+
```
459
+
460
+
::: info Note
461
+
Calling `faker.clone()` is idempotent. So you can call it as often as you want, it doesn't have an impact on the original faker instance.
0 commit comments