mswjs / http-middleware

Spawn an HTTP server from your request handlers or apply them to an existing server using a middleware.
https://npm.im/@mswjs/http-middleware
109 stars 13 forks source link

Question: Can you modify the mocked handlers per request #38

Closed JoshAwaze closed 10 months ago

JoshAwaze commented 10 months ago

Hi, before I start to use your library I was wondering if it supported the ability to change the underlying mocked handlers for each test.

E.g.

// some pseudo code

test('some happy path') {
page.goTo("/login"); // this should be mocked with default 200 response

}

test('some failure path') {

// override the response to simulate failure
  server.use(
    http.get('/login', () => {
      return new HttpResponse(null, { status: 500 })
    })
  )

}
kettanaito commented 10 months ago

Hi. Initially, I thought it was possible but since createMiddleware function spreads the handlers array, it produces a new array. You wouldn't be able to modify it even if you modify the original handlers you spread. What you describe is not possible at this moment.