Question: What would be the downside of having two msw server instances running at the same time? #821
-
Environmentn/a Request handlersSo we have individual test files that'll spin up their own msw servers with // test-specific-file.test.ts
const server = setupServer(...handlers);
describe('a test', () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
test('api requests', () => {
// ...
})
}) Then in our jest setup, we spin up an additional server before each test run to mock out any accidentally non-mocked routes:
Actual requestMostly with Current behaviorThe tests work correctly, but the downside of doing so isn't clear to me, and that's what I'm hoping to answer through this GH issue. Expected behaviorThat the tests run properly. Screenshotsn/a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey, @brianzhou13. There's little reason to have multiple instances of Instead, create a single import { setupServer } from 'msw'
export const server = setupServer(/* global handlers, if any */) // setupFilesAfterEnv.js
import { server } from './server'
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close()) // some.test.js
import { msw } from 'msw'
import { server } from './server'
beforeEach(() => {
server.use(rest.get('/', resolver))
}) Note that if you decide to use |
Beta Was this translation helpful? Give feedback.
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.