Skip to content

Commit

Permalink
fix(faker): specify refDate to fix value of faker.date.past()
Browse files Browse the repository at this point in the history
As shown in the following code, the result changes even if the seed is added:
```
> const { faker } = require("@faker-js/faker")
undefined
> faker.seed(0)
0
> const a = faker.date.past().toISOString()
undefined
> a
'2022-03-08T21:45:15.158Z'
> faker.seed(0)
0
> const b = faker.date.past().toISOString()
undefined
> b
'2022-03-08T21:45:30.411Z'
> a === b
false
```

This is because the default refDate passed in the second argument of faker.date.past() is now, so we can fix it at a specific date.

ref: faker-js/faker#859
  • Loading branch information
MH4GF committed Sep 25, 2022
1 parent 539d543 commit a81c1de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
if (!customScalar || !customScalar.generator) {
if (foundType.name === 'Date') {
return opts.dynamicValues
? `faker.date.past().toISOString()`
: `'${faker.date.past().toISOString()}'`;
? `faker.date.past(1, new Date(2022, 0)).toISOString()`
: `'${faker.date.past(1, new Date(2022, 0)).toISOString()}'`;
}
return opts.dynamicValues ? `faker.lorem.word()` : `'${faker.lorem.word()}'`;
}
Expand Down
Loading

0 comments on commit a81c1de

Please sign in to comment.