-
Notifications
You must be signed in to change notification settings - Fork 46
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(deps): switch to faker from casual #92
Conversation
✅ TODO:
📝 memo:
Check compatibility
|
Because faker almost completed migrating to TypeScript so that DefinitelyTyped no longer needs to maintain its external [@types/faker](https://www.npmjs.com/package/@types/faker) package. ref: https://fakerjs.dev/about/announcements/2022-01-14.html
Since the seed value changes with the switching from casual to faker, the snapshots needs to be updated. I confirm that the format does not change significantly, despite the differences.
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
2026f75
to
3bb083b
Compare
I grep'd and changed only the part I replaced this PR to faker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the implemetation!
I have a few comments about breaking changed, I'm not a fan of using casual and faker at the same time, especially because the generated output could have breaking change
I would see few options to reduce the breaking changes for users:
- Use faker all the way. This includes generation when
dynamicValues
istrue
orfalse
. As the output will be different already, I think we can break the scalar option too. This option is the most user's impacting but at least we don't have legacy code - Use faker only when
dynamicValues
istrue
. We keep casual for the rest of the code, so whendynamicValues == false
we call casual, and it won't change the output of generated mocks. It comes with the downside of using 2 libraries for value generation and could be confusing when users start usingdynamicValues
. This would however be confusing for custom scalars - Have an option to select the generator library. We could have an option like
generatorLibrary: 'faker' | 'casual'
(default:casual
). This way, we can introduce the changes smoothly. Users not touchinggeneratorLibrary
won't have any breaking change, but those wanting last features can benefit fromfaker
. It would also impact custom scalars. Then for the next major version, we can deprecatecasual
and change the default fromcasual
tofaker
. This leaves some time to developers to upgrade
I kind of like option 3 even if it's more job. I think we could create a helper file (or class) to simplify the code. This class would generate the values or function calls for us based on the generatorLibrary
option
Something like this:
class ValueGenerator {
constructor(private options: {dynamicValues: boolean, generatorLibrary: 'casual' | 'faker'}) {}
word() {
if (this.options.generatorLibrary === 'faker') {
return this.options.dynamicValues ? `faker.lorem.word()` : `'${faker.lorem.word()}'`;
} else {
return this.options.dynamicValues ? `casual.word` : `'${causl.word}'`;
}
}
}
const name = opts.currentType.name.value; | ||
const casedName = createNameConverter(opts.typenamesConvention, opts.transformUnderscore)(name); | ||
switch (name) { | ||
case 'String': | ||
return opts.dynamicValues ? `casual.word` : `'${casual.word}'`; | ||
return opts.dynamicValues ? `faker.lorem.word()` : `'${faker.lorem.word()}'`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should call casual
if dynamicValues
is false ?
@ardeois Thank you for your thoughtful comments! As @JimmyPaolini points out here #90 (comment) , the current implementation has a problem that is no different from a destructive change because the output results are different. As a solution to this, I think that your suggested option 3 is a better choice. It makes sense that most users would be satisfied with casual and only those who want to use faker can choose it of their own volition. I'll try to add the generatorLibrary option after work! Also the ValueGenerator class you suggested looks good. |
Closing this PR as #93 cover it already with retro-compatible config to avoid breaking changes |
closes #90
What
Switch casual with faker, which was used in the generation process of the mocked temporary values.
Context
casual dependents on the node API(e.g. fs) and cannot be made to work in the browser.
ref: #90
Problem
This library has the feature for custom generators to embed in casual property calls, and switching to faker would be a breaking change.
Since it is not good to force many users to migrate for the dynamicValues feature that few users use yet, I think it is better to migrate only the other mock functions on a temporary basis.