Skip to content

Instantly share code, notes, and snippets.

@ahayes91
Last active June 12, 2021 20:47
Show Gist options
  • Save ahayes91/7ed15a6df36d6fc3484cae4d38b78300 to your computer and use it in GitHub Desktop.
Save ahayes91/7ed15a6df36d6fc3484cae4d38b78300 to your computer and use it in GitHub Desktop.
Helper file for setting up a Mock Service Worker node server for integration testing
import { setupServer } from 'msw/node';
export default function getAndSetupServer(handlers = []) {
const server = setupServer(...handlers);
beforeAll(() => {
server.listen({
onUnhandledRequest(req) {
const errorMessage = `Found an unhandled ${req.method} request to ${req.url.href}`;
console.error(errorMessage);
throw errorMessage;
},
});
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});
return server;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment