Skip to content

Runtime request handlers in the browser (Playwright, Remix) #2318

Answered by kettanaito
oscard0m asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @oscard0m.

If you are in Playwright, you are running tests in the browser. For that, you need to deal with worker, not server.

  1. Follow the browser integration to set up MSW in your app.
  2. Expose the worker on window (or any other method of your choosing to have it available during the tests).
  3. During the test, reference window.worker to add runtime handlers.
test('my test', async ({ page }) => {
  await page.evaluate(() => {
    const { worker, http } = window.msw
    worker.use(http.get('/override', resolver))
  })
})

Note that I'm also exposing http on window to access it in the same context as window.worker.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@oscard0m
Comment options

@kettanaito
Comment options

@oscard0m
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants