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 can be a function #1329

Closed
purefun opened this issue Apr 25, 2024 · 2 comments · Fixed by #1375
Closed

MSW: overrideResponse can be a function #1329

purefun opened this issue Apr 25, 2024 · 2 comments · Fixed by #1375
Labels
enhancement New feature or request mock Related to mock generation
Milestone

Comments

@purefun
Copy link

purefun commented Apr 25, 2024

Currently, a msw mock handler would be:

import { HttpResponse, HttpResponseResolver, delay, http } from 'msw'

export const getSomethingMockHandler = (overrideResponse?: SomethingResponse) => {
  return http.get('/something', async () => {
    await delay(1000);
    return new HttpResponse(JSON.stringify(overrideResponse ? overrideResponse : getSomethingResponseMock()),
      {
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        }
      }
    )
  })
}

But it would be nice if overrideResponse is a function. So that we can produce response programmatically.

export const getSomethingMockHandler = (overrideResponse?: (resolver: HttpResponseResolver) => SomethingResponse | Promise<SomethingResponse>) => {
  return http.get('/something', async (resolver) => {
    await delay(1000);
    const response = overrideResponse ? overrideResponse(resolver) : getSomethingResponseMock()
    return new HttpResponse(JSON.stringify(response)),
      {
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        }
      }
    )
  })
}
@melloware melloware added the mock Related to mock generation label Apr 25, 2024
@melloware melloware changed the title Feature Request: msw overrideResponse can be a function MSW: overrideResponse can be a function Apr 25, 2024
@melloware
Copy link
Collaborator

@soartec-lab is this the same issue you just fixed in 6.28.0? #1293

@soartec-lab soartec-lab added the enhancement New feature or request label Apr 30, 2024
@soartec-lab
Copy link
Collaborator

@melloware
No, this is separate from #1293. I'll add an enhancement label to this.

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.

3 participants