New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support local server for complex testing scenarios #79
Comments
This is how I see such setup working: // Do not confuse with the "spawnServer" utility already present
// to spawn a Webpack Dev Server. Naming is hard.
import { spawnServer } from '../utils/spawnServer'
let server
beforeAll(() => {
server = spawnServer(app => {
app.post('/users', (req, res) => {
res.status(301).json({ actual: 'response' })
})
})
})
afterAll(() => {
// Needs to return a Promise.
// If Express API doesn't do that, wrap in a custom function.
return server.instance.close()
})
describe('Test case', () => {
it("scenario", async () => {
// Abstracted API to get the proper route in order to not rely
// on exact server port number.
const REQUEST_URL = server.getRoute('/users')
api.page.evaluate((url) => fetch(url, { method: 'POST' }), REQUEST_URL)
// continue with assertions...
})
}) |
Since MSW already has a spawnServer('/path/to/file.mocks.ts', {
withRoutes(app) {
app.post('/api/mock', (req, res) => {
res.status(200).json({ response: true })
})
}
}) The benefits we get from entwining this logic into existing
|
What:
I suggest to add a support for spawning a local server for the purpose of testing.
Why:
There are complex test scenarios (such as "response patching") that assert against an actual server implementation. Relying on third-party servers brings a factor of instability to the CI pipeline of MSW. Having a local server for the sake of testing sounds like a viable option.
How:
0
for the server to always have a dedicated port)response-matching.test.ts
and any other tests that require an actual server to use the introduced local server implementationafterAll()
test suites are finishedThe text was updated successfully, but these errors were encountered: