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

mocked responses aren't unique #1680

Closed
borisyordanov opened this issue Oct 31, 2024 · 3 comments · Fixed by #1686
Closed

mocked responses aren't unique #1680

borisyordanov opened this issue Oct 31, 2024 · 3 comments · Fixed by #1686
Assignees
Labels
mock Related to mock generation
Milestone

Comments

@borisyordanov
Copy link
Contributor

What are the steps to reproduce this issue?

Generated mocks return duplicate strings (and other values) which make it hard to write reliable tests.

export const getShowPetByIdMock = (overrideResponse?: Partial<Type>): Type => ({
   id: faker.number.int({ min: undefined, max: undefined }),
   name: faker.word.sample(),
   tag: faker.helpers.arrayElement([faker.word.sample(), undefined]),
   ...overrideResponse,
 });

What happens?

Our test suite is flaky because mocked data is often duplicated.

We follow Testing library's guiding principles and query priorities so we often query by role name/text.
https://testing-library.com/docs/guiding-principles
https://testing-library.com/docs/queries/about/#priority

Orval generates mock string values as faker.word.sample() in msw responses. This method does not guarantee uniqueness. Also because orval uses the default length of the word faker often generates string of only 2 chars which increases the likelihood of duplicates.

https://github.com/orval-labs/orval/blob/master/packages/mock/src/faker/getters/scalar.ts#L253C20-L253C39

What were you expecting to happen?

I don't expect 100% unique values, but with some small modifications the mocks could become much more useful. Consider setting a larger default length of the strings you generate or even using a different Faker method like simpleFaker.string.uuid()

Any logs, error output, etc?

     × SiteMaterialDetails > renders site material details 1070ms
       → Found multiple elements with the text: so

Any other comments?

Being able to set msw/faker settings in orval.config.js would be a fantastic quality of life improvement.

What versions are you using?

Please execute npx envinfo --system --npmPackages orval,zod,axios,msw,swr,@tanstack/react-query,@tanstack/vue-query,react,vue and paste the results here.

@melloware melloware added the mock Related to mock generation label Oct 31, 2024
@melloware
Copy link
Collaborator

PR is welcome to improve the code!

@borisyordanov
Copy link
Contributor Author

@melloware please review #1686

@melloware melloware added this to the 7.2.1 milestone Nov 1, 2024
@melloware
Copy link
Collaborator

Thanks for the PR looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mock Related to mock generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants