orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3.18k stars 336 forks source link

mocked responses aren't unique #1680

Closed borisyordanov closed 3 weeks ago

borisyordanov commented 3 weeks ago

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 commented 3 weeks ago

PR is welcome to improve the code!

borisyordanov commented 3 weeks ago

@melloware please review https://github.com/orval-labs/orval/pull/1686

melloware commented 3 weeks ago

Thanks for the PR looks good!