Allow override same endpoint on setup #1410
-
Hi folks, msw is wonderful and I enjoy using it in my daily development. Here's one thing that I think it would make things simpler for this unit-testing use case: Say I have several endpoints that needs to be mocked for every test case, e.g. the // test-utils.js
import { rest } from 'msw';
import { setupServer as mswSetupServer } from 'msw/node';
export function setupServer(...args) {
return setupServer(
rest.get('/api/current-user', (req, res, { json }) => res(json({}))),
...args
)
}
export { rest }; The only problem is that when I need a different response result for a test suite (not for a single test case), I can't override the previously defined endpoint right on setup import { setupServer, rest } from '../test-utils';
const server = setupServer(
rest.get('/api/current-user', (req, res, { json }) => res(json({ id: 100 }))) // This dosen't work
) In other words, if there're multiple same endpoints in const server = setupServer(
rest.get('/api/current-user', (req, res, { json }) => res(json({ id: 1 }))), // This works
rest.get('/api/current-user', (req, res, { json }) => res(json({ id: 100 }))) // This won't work
) What I have to do now is to manually use const server = setupServer();
beforeEach(() => {
server.use(
rest.get('/api/current-user', (req, res, { json }) => res(json({ id: 100 }))) // 🫤
)
}); Any thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hey, @SevenOutman. Thanks for raising this. I think your abstracting things a little but too high the chain. Instead of wrapping the entire export const defaultUserHandlers = [
rest.get('/api/current-user', resolver),
// ...as many as you need to describe a happy path.
] Then, in tests just provide this handlers list to the import { setupServer } from 'msw/node'
import { defaultUserHandlers } from './handlers/user'
const server = setupServer(...defaultUserHandlers) This should cover the repetition for the test cases that rely on the happy path handlers defined in Now, when it comes to overrides, you can provision them in any supported way in MSW:
For example, if your entire test suite needs a differ user behavior, choose the first option: import { rest } from 'msw'
import { setupServer } from 'msw/node'
import { defaultUserHandlers } from './handlers/user'
const server = setupServer(
// Note: MSW applies handlers left-to-right
// so you have to put first the handlers you wish
// to take precedence (your overrides).
rest.get('/api/current-user', resolveDifferentlyForThisTest),
...defaultUserHandlers,
)
My main suggestion here is to abstract at the array of handlers level because you can leverage the standard Array API to extend your abstraction vs having to design this extension by yourself if you choose functions. |
Beta Was this translation helpful? Give feedback.
Hey, @SevenOutman. Thanks for raising this.
I think your abstracting things a little but too high the chain. Instead of wrapping the entire
setupServer
call, abstract the handlers.Then, in tests just provide this handlers list to the
setupServer
call:This should cover the repetition for the test cases that rely on the happy path handlers defined in
defaultUserHandlers
.Now, when it comes to overrides, you can p…