At WegoPro, I spent an entire afternoon asking another engineer to break the backend on purpose so my frontend test could pass.
The backend was not broken. My test setup was.
The scenario nobody could hit
Our booking flow had two confirmation steps. First, the user approved the travel request. Then, at final checkout, the system re-fetched the flight quote one last time to make sure the airline had not changed the fare or dropped the seat.
Most of the time, the price stayed the same. The checkout finished cleanly.
The branch I needed to verify was the rare one: the airline returns an updated quote between approval and checkout. The frontend was supposed to halt the payment, display the new price diff clearly, and ask the user to re-confirm before charging the company card.
On staging, that almost never happened naturally. Live airline test sandboxes rarely fluctuate on demand, and clicking through the UI in staging just kept giving me unchanged quotes.
So I went over to the engineer who owned the flight service: "Can you return a price diff on the re-check endpoint for my test user?"
He changed a flag or hardcoded a higher amount in his local branch or test environment. I ran my Playwright test. It reached the checkout screen, caught the diff, showed the modal, and passed.
Then I needed to test the error recovery branch. "Can you make it return a 409 or a sold-out status now?"
He changed it again. Then someone else on the team triggered an automated test run that failed because the flight quote mock was hardcoded to return errors. So he reverted it. Ten minutes later, I had to fix an assertion in my UI flow, and I was back at his desk: "Could you flip it back one more time?"
It felt ridiculous. A frontend E2E test should verify user interactions and rendering logic. Instead, my test run was held hostage by manual backend state manipulation.
What was actually wrong
At the time, my mental model for an E2E test was rigid: if it is an end-to-end test, everything must run against the real staging services, end to end.
That sounds noble until you hit boundary conditions. Staging data is noisy and shared. Simulating external upstream shifts—like a third-party airline raising a seat price by $40 mid-session—requires jumping through operational hoops just to make the frontend see a valid API response.
The issue was not the backend logic. The flight service already had its own unit and integration tests verifying that it emitted updated quotes correctly.
My frontend test was not trying to verify whether the airline API worked. It was trying to verify how our UI handled a specific, contract-valid state. Yet I was forcing a colleague to do manual test setup in the backend just so my browser runner could see that JSON payload.
The tool I found too late
I left WegoPro before I ever touched Mock Service Worker.
When I eventually learned how MSW operates, the realization was painful. It intercepts network traffic at the browser's Service Worker level. The application code makes a real fetch or axios call, untouched. The browser network stack behaves normally. But before the request leaves for the wire, MSW can intercept it and return an exact mock response.
If I had known about MSW back then, I could have defined reusable contract handlers and overridden just that single checkout re-check inside the Playwright test:
import { http, HttpResponse } from 'msw'
import { test, expect } from '../support/playwright-msw'
test('halts checkout when flight quote changes mid-flow', async ({ page, network }) => {
// Override the flight quote endpoint with MSW for this specific test case
network.use(
http.get('*/api/v1/flights/quote/:id', ({ request }) => {
const url = new URL(request.url)
// Let initial steps pass through or return default baseline
if (url.searchParams.get('step') !== 'checkout') {
return HttpResponse.json({ status: 'CONFIRMED', price: 420 })
}
// Inject the rare contract-valid price jump at final confirmation
return HttpResponse.json({
quoteId: 'quote-flight-sin-hnd',
status: 'PRICE_CHANGED',
originalFare: 420,
updatedFare: 465,
currency: 'SGD',
validUntil: new Date(Date.now() + 15 * 60 * 1000).toISOString()
})
})
)
await page.goto('/trips/book/quote-flight-sin-hnd')
await page.getByRole('button', { name: 'Confirm & Proceed to Checkout' }).click()
// Verify UI displays diff banner without charging the corporate card
const modal = page.getByRole('dialog', { name: 'Flight Price Updated' })
await expect(modal).toBeVisible()
await expect(modal.getByText('$465 SGD')).toBeVisible()
await expect(modal.getByRole('button', { name: 'Accept New Price & Pay' })).toBeEnabled()
})
The key piece here is the network.use(...) fixture from @msw/playwright.
Instead of spinning up separate mock servers or hacking window objects across browser processes, it bridges MSW directly into Playwright's test lifecycle:
-
Scoped overrides:
network.use(...)prepends a temporary request handler strictly for this single test. The moment this test finishes, the override tears down automatically. The rest of the test suite runs untouched against normal baseline handlers. -
Contract-level mocking: The handlers use standard MSW syntax (
http.get,HttpResponse.json). You can write these handlers once and share them between unit tests in Vitest and E2E suites in Playwright. - Zero staging collisions: No poking the backend owner. No shared staging flags. No CI runs failing because someone hardcoded an error response into staging.
The browser runs through the real UI flow: authentication, navigation, form filling, and the approval click. But when the confirmation screen fires the final re-check query, MSW intercepts it at the network layer and returns the price diff deterministically.
Contract vs integration
There is always an objection to mocking in E2E: "If you mock the response, is it still really an E2E test?"
It is not a full-system integration test. But frontend tests and backend integration tests have different jobs.
The backend integration test proves that when third-party providers send updated fares, our service parses them and outputs the right schema.
The frontend test proves that given that schema, the user interface does not drop the state, miscalculate the diff, or let the user book an unconfirmed fare.
Tying those two tests together into a single fragile staging run gave us the worst of both worlds: slow feedback, flaky suites, and engineers interrupting each other just to test an edge case.
I still think about that afternoon whenever I see an E2E suite that requires complex database seeding or manual staging flags just to trigger a rare response.
The backend was never the bottleneck. I was just asking it to do test setup for me.

Top comments (0)