-
-
Notifications
You must be signed in to change notification settings - Fork 950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(date): introduce global refDate instead of passing it every time #859
Comments
This is because the |
Indeed, it has to do with calling the Line 13 in b10f00c
|
IMO this is not a bug. With a fixed If you pass a |
Thanks for the snapshots tip, but I don't want to set this manually in each snapshot whenever a date is involved. That's why the faker.seed is useful. Also the documentation states: "If you want consistent results, you can set your own seed", but if faker.date.past() changes each time, that is not very consistent. When a seed is set, then instead of assigning |
We could do that of course or even hardcode it. However we have to weight two different concerns here. If you are calling If you are interested in explicitly setting the |
Setting the |
+1 |
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
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
Describe the bug
When using faker.seed with a value, e.g.
faker.seed(123)
, faker should always give back the same values for the faker functions, but this is not the case for faker.date.past(). It uses a fixed amount of time to go back, but that's always the current date minus that fixed time. So for example calling faker.date.past() today would give a different result when calling it tomorrow.The issue is not present when providing the parameters maxYears and refDate, e.g.
faker.date.past(30, new Date(2022,0))
.Reproduction
faker.seed(123);
faker.date.past()
(without parameters)faker.date.past()
See https://stackblitz.com/edit/typescript-qiclti for a reproduction.
Additional Info
@faker-js/faker version used: 6.1.2
The text was updated successfully, but these errors were encountered: