-
-
Notifications
You must be signed in to change notification settings - Fork 961
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
Provide a function to reset faker.unique()
#371
Comments
IMO we should go for a parameter passed unique store. |
I have a case to make for the global store. We are using using factory pattern, and the factory runs in a scope of single instance of the model. Therefore, the factory doesn't know if it's creating multiple instances or just one (it wouldn't help even if it did know). // Book Factory
defineFactory(
BookEntity,
(
faker: Faker,
context: {
title?: string
},
) => {
const book = new BookEntity()
book.title = context.title || faker.unique(faker.lorem.words)
return book
},
) // Inside a test body
const books = await factory(BookEntity)().createMany(3) We could pass the faker instance (or the dictionary) as context to the factory, but this would result in unnecessary boilerplate code for something that should be very trivial and easy to achieve. It makes the test writing experience unnecessarily painful. beforeEach(async () => {
DB.clear()
Faker.reset() // This is a static method, not a instance method
}) For inspiration: https://github.com/faker-ruby/faker#ensuring-unique-values |
@fshafiee Please open a new feature request issue or a discussion, but please don't write especially such long answers into already merged and closed historical PRs, as the info you provide will go under here. |
Clear and concise description of the problem
Currently
unique
uses a global constant that remembers which values where already provided back so these will not be provided back a second time.It seems there is not possibility to reset this or provide a dedicated cache.
Suggested solution
Provide the possibility to create a dedicated store that can be used.
Alternative
No response
Additional context
faker/src/vendor/unique.ts
Line 5 in 730ca6a
The text was updated successfully, but these errors were encountered: