Skip to content
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

Closed
wants to merge 8 commits into from

Conversation

MH4GF
Copy link
Contributor

@MH4GF MH4GF commented Sep 24, 2022

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.

@MH4GF
Copy link
Contributor Author

MH4GF commented Sep 24, 2022

✅ TODO:

📝 memo:

Check compatibility

casual casual result faker faker result
casual.seed(0) N/A faker.seed(0) N/A
Math.round(casual.double(0, 10) * 100) / 100 3.8 faker.datatype.float({ min: 0, max: 10, precision: 0.1 }) 6.4
casual.integer(0, 9999) 6130 faker.datatype.number({ min: 0, max: 9999 }) 9962
casual.uuid '99bd9d8d-79a6-474e-8f46-6cc8796ed151' faker.datatype.uuid() '89bd9d8d-69a6-474e-8f46-7cc8796ed151'
casual.boolean true faker.datatype.boolean() true
new Date(casual.unix_time) 1970-01-08T09:31:42.245Z faker.date.past() 2022-02-14T08:58:38.892Z
casual.word 'laborum' faker.lorem.word() 'harum'

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
I grep'd and changed only the part I replaced this PR to faker.
@MH4GF MH4GF marked this pull request as ready for review September 25, 2022 12:21
@ardeois ardeois self-assigned this Sep 26, 2022
Copy link
Owner

@ardeois ardeois left a 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:

  1. Use faker all the way. This includes generation when dynamicValues is true or false. 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
  2. Use faker only when dynamicValues is true. We keep casual for the rest of the code, so when dynamicValues == 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 using dynamicValues. This would however be confusing for custom scalars
  3. 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 touching generatorLibrary won't have any breaking change, but those wanting last features can benefit from faker. It would also impact custom scalars. Then for the next major version, we can deprecate casual and change the default from casual to faker. 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()}'`;
Copy link
Owner

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 ?

@MH4GF
Copy link
Contributor Author

MH4GF commented Sep 27, 2022

@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.
Since the purpose of this PR will be different from this one, I will publish it as a separate PR.

@ardeois
Copy link
Owner

ardeois commented Sep 29, 2022

Closing this PR as #93 cover it already with retro-compatible config to avoid breaking changes

@ardeois ardeois closed this Sep 29, 2022
@MH4GF MH4GF deleted the switch-to-faker-from-casual branch September 30, 2022 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dynamicValues option does not work in browser
2 participants