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

Mock TypeError custom override #1202

Closed
dipsaus9 opened this issue Feb 5, 2024 · 7 comments · Fixed by #1229
Closed

Mock TypeError custom override #1202

dipsaus9 opened this issue Feb 5, 2024 · 7 comments · Fixed by #1229
Assignees
Labels
bug Something isn't working mock Related to mock generation
Milestone

Comments

@dipsaus9
Copy link
Contributor

dipsaus9 commented Feb 5, 2024

When using the override mock option the TypeCheck is too strict

With the release of 6.24 orval introduced the return type for the mws files, see #1171.
When using the override option for certain opperations the TypeCheck is too strict, without any options to set the type.
As orval uses js to parse the document we can not add extra type declarations to the config. This breaks our

  1. Define a override operation in the orval.config.js
  2. Set the option mock to true
  3. Get a type error when override Enums or literals

What happens?

TypeErrors in the .msw.ts file

What were you expecting to happen?

Not sure what the best solution is. The return type is a nice feature, but maybe we should be able to override the return value, or not set it in case of the override function in the config. As orval uses JavaScript and not TypeScript we can not strictly type the return value in the config.

Any logs, error output, etc?

Not at the moment, if you need more details I can add it.

Any other comments?

Open for suggestions.

@melloware melloware added the bug Something isn't working label Feb 5, 2024
@melloware melloware added this to the 6.25.0 milestone Feb 5, 2024
@melloware
Copy link
Collaborator

@soartec-lab assigned this to you as you fixed the original ticket.

@melloware melloware changed the title TypeError custom override Mock TypeError custom override Feb 5, 2024
@melloware melloware added the mock Related to mock generation label Feb 5, 2024
@soartec-lab
Copy link
Collaborator

Similar to #1201

@soartec-lab
Copy link
Collaborator

@dipsaus9
Thank you for made this report.
I would like to understand this problem correctly, so could you please tell me the error message that is output?

@soartec-lab
Copy link
Collaborator

I have fixed the issue where it did not work correctly by #1205 when enum types were included.

@dipsaus9
Copy link
Contributor Author

dipsaus9 commented Feb 6, 2024

I'll test it as the release is out there.
To clarify the issue:

With the return type now added to the .msw.ts file, the type check is more strict.
As an example, if the Schema of Dog has an literal or enum as a return value, orval uses the following code:

faker.helpers.arrayElement([
    faker.helpers.arrayElement(Object.values(DogType)),   // DogType is not imported
    undefined,
  ])

This is great.

But if you want to override this value with the override function we can not type the enum. As an example in the config we currently have.

override: {
	operations: {
		api_countries_get_collection: {
			mock: {
				data: () => {
					const countries = ["GB"] // We limit the Enum list here due to testing security

					return countries
				}
			},
		},
	},
}

The expected return type is a list of enum.

In the previous version this would result in no errors. With the strict return type the countries const now types to a string.
And we have no way to update this to a typed value as the config uses common js.

Hope this clarifies the issue.

@soartec-lab
Copy link
Collaborator

Hi, @dipsaus9

Since we released "v6.25.0" this issue has been fixed 👍

@dipsaus9
Copy link
Contributor Author

@soartec-lab Release v6.25.0 did not fix our issue.
I opened a PR that can be a fix #1229

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

Successfully merging a pull request may close this issue.

3 participants