Skip to content

Question: What would be the downside of having two msw server instances running at the same time? #821

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

You must be logged in to vote

Hey, @brianzhou13.

There's little reason to have multiple instances of setupServer calls. If anything, that will patch the request-issuing modules multiple times (which is how request interception works in Node.js), which may lead to all sorts of unexpected behavior.

Instead, create a single setupServer integration point in your global test setup and modify it per test suite, if necessary. That way you get the same control over your API with a single setup.

import { setupServer } from 'msw'

export const server = setupServer(/* global handlers, if any */)
// setupFilesAfterEnv.js
import { server } from './server'

beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
af…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by brianzhou13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
bug Something isn't working scope:node Related to MSW running in Node
2 participants
Converted from issue

This discussion was converted from issue #820 on July 19, 2021 20:28.