Last active
June 12, 2021 20:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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