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

MSW: overrideResponse should allow async functions #1389

Closed
severinh opened this issue May 20, 2024 · 0 comments · Fixed by #1390
Closed

MSW: overrideResponse should allow async functions #1389

severinh opened this issue May 20, 2024 · 0 comments · Fixed by #1390
Assignees
Labels
enhancement New feature or request mock Related to mock generation
Milestone

Comments

@severinh
Copy link
Contributor

severinh commented May 20, 2024

What are the steps to reproduce this issue?

In MSW, a mock handler can access the response resolver info. For example, it can access the request body to change the response based on the request. Access to that request body is only possible through an async function call to json().

http.post("/some/path", async ({ request }) => {
  const requestJson = await request.json();
  // Do something with the request, such as producing a response based on it.
  return ...
});

In Orval, PR #1375 made it possible for the override in a MSW mock handler to be a function, and Orval passes the response resolver info to that function. However, Orval requires a synchronous method that returns a response object. It does not allow async functions.

Hence, it's not possible to migrate the above to Orval.

getSomePathMockHandler(async ({ request }) => {  // This does not type-check. Orval does not allow a Promise<SomeResponse> return type.
  const requestJson = await request.json();
  // Do something with the request, such as producing a response based on it.
  return ...
});

What happens?

The above code does not type-check, because Orval does not allow response type to be Promise<SomeResponse>.

What were you expecting to happen?

The above code should type-check. It should be possible to pass both a sync or async method, in the same way MSW allows this.

Any logs, error output, etc?

N/A

What versions are you using?

Operating System: MacOS
Package Version: 0.29.1
Browser Version: Node.js 20

This should be trivial to fix, so I'll open a simple PR.

@melloware melloware changed the title overrideResponse in msw should allow async functions MSW: overrideResponse should allow async functions May 20, 2024
@melloware melloware added mock Related to mock generation enhancement New feature or request labels May 20, 2024
@melloware melloware added this to the 6.30.0 milestone May 20, 2024
soartec-lab pushed a commit that referenced this issue May 25, 2024
…1389) (#1390)

* fix: allow async overrideResponse to be passed to msw handler

* Add back trailing whitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mock Related to mock generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants