Skip to main content

Mock Service Worker vs a Browser Extension (tweak)

ยท 7 min read
Daniel Caldas
Co-founder

Sooner or later every frontend team builds a screen against an API that does not exist yet, is down, or cannot be made to return the case you need. There are two broad ways to fake it in the browser: put the mocks inside your app with a library like Mock Service Worker (MSW), or put them outside your app with a browser extension. They solve the same problem from opposite ends, and picking the wrong one costs you either setup time or test coverage.

The two approaches in one paragraph eachโ€‹

Mock Service Worker registers a service worker in your app (and an interceptor in Node.js for tests). You write request handlers in JavaScript, commit them next to your code, and every request that matches a handler gets a mocked response. The mocks are part of the codebase: reviewed, versioned and run in CI.

A browser extension sits in the browser, not in the app. You open its panel, write a rule ("GET /api/orders returns this JSON with status 500"), and the extension answers matching requests on whatever page is open. The mocks are part of your browser: no code, no build, and they work on any site, including one you do not have the source for.

Side by sideโ€‹

Mock Service WorkerBrowser extension (tweak)
Where mocks liveIn the repository, as codeIn the browser, as rules
SetupInstall the package, generate the worker, start itInstall the extension
Needs the app's sourceYesNo
Works on staging and productionOnly if you ship MSW thereYes, on any page
Unit and integration tests (Node)Yes, first classNo
End-to-end testsYes, with the worker enabled in the test buildNot the goal: it is for manual testing
Shared with the teamThrough GitThrough an exported JSON file
Who can use itDevelopersDevelopers, QA, designers, product managers
Changing a mockEdit code, wait for reloadEdit the rule, refresh the page

Where Mock Service Worker winsโ€‹

Automated tests. This is MSW's home ground. The same handlers run in Vitest or Jest through msw/node, so your component tests hit realistic HTTP responses instead of a mocked fetch. An extension cannot help a test runner that has no browser.

One source of truth. Handlers written once serve unit tests, Storybook, local development and the CI suite. When the API contract changes, the handler changes in the same pull request, and a reviewer sees it.

Every developer gets the same world. Clone, install, run: the mocks are there. Nobody has to import a file or remember which rules to switch on.

Long-lived fake backends. If the backend will not exist for weeks, a full set of handlers (with in-memory state, pagination, auth) is a better investment than a pile of one-off rules.

Where a browser extension winsโ€‹

No code change, no setup. There is nothing to install in the project, no worker file in public/, no if (process.env.NODE_ENV === 'development') around the startup. This matters most in a codebase you do not own, or one where adding a dev dependency is a conversation.

Staging and production. A bug reported on production is reproduced on production. MSW is normally stripped from production builds, so it cannot help there. An extension can mock a response on the live site, and only in your browser.

One-off cases. "What does this page look like when the cart has 200 items?" "What happens on a 503?" Writing, wiring and later deleting a handler for a question you will ask once is overhead. A rule takes seconds and is switched off when you are done.

People who do not write code. QA testing an error screen, a designer checking an empty state, a PM preparing a demo: they can all do it from a panel, without a local checkout.

Modifying the real response. MSW can call the real server and patch the answer, but you write that logic yourself. In tweak a Modify rule calls the real server and changes one field, the status or a header, and leaves the rest as it came.

Requests your app does not make with fetch. MSW is built for your app's API calls and lets page navigations through. Browser-level rules like tweak's headers, redirect and block rules are applied by the browser itself, so they cover them.

Where each one breaksโ€‹

MSW breaks when:

  • the page is not yours, or you cannot change its build;
  • you need it on a deployed environment where the worker is not shipped;
  • a service worker is already registered at the same scope, or the app runs on a non-secure origin where service workers are not allowed;
  • the person who needs the mock does not write JavaScript.

A browser extension breaks when:

  • you need the mock in a headless test runner or in CI;
  • the whole team must see the same mocks without doing anything;
  • the mocks need to evolve with the code and be reviewed in pull requests.

Using both on the same projectโ€‹

Most teams that try both end up keeping both, each for its own job:

  1. MSW for the contract. Handlers for the happy paths and the important error cases, used by the test suite and by npm run dev. They are the shared baseline.
  2. The extension for exploration. On top of that baseline, or on staging where MSW is off, you use rules for the questions nobody wrote a handler for: a 429 with a Retry-After header, a payload with a missing field, a slow endpoint, a broken image.
  3. Promote what sticks. When a one-off rule turns into a case you check every release, write it as a handler and commit it.

If both are active on the same page, remember that the extension sees the request first: tweak wraps fetch and XMLHttpRequest in the page, so a matching tweak rule answers before the request ever reaches MSW's worker. Switch one off when results look surprising.

Pricing comparedโ€‹

MSW is free and open source (MIT license), with no paid tiers.

tweak is free to start, no account required: up to 12 Mock rules, plus header, redirect and block rules. Tweak Basic ($2/month or $18/year) adds unlimited rules and Modify rules that patch the real response; Tweak Max ($4/month or $36/year) adds variables, environments, JavaScript hooks and Pause & Edit.

How to decideโ€‹

  • You are writing automated tests: MSW.
  • You need the same mocks for the whole team, versioned with the code: MSW.
  • You are reproducing a bug on staging or production: browser extension.
  • You need an answer in the next five minutes: browser extension.
  • The person mocking does not write code: browser extension.
  • The backend will not exist for a month: MSW for the baseline, an extension for the edge cases.

Try the extension sideโ€‹

Install tweak, open the panel on any page, and create a Mock rule for the request you want to fake. No account, and Mock rules are free. When you want a rule set the team can reuse, export it and commit the file next to your code.

More comparisons: tweak vs Requestly, another browser extension, and tweak vs Proxyman, a desktop proxy. For a ranked list, see the best Chrome extensions to mock API responses, or every comparison on the alternatives page.

If you liked this article, consider sharing (tweeting) it to your followers.



Did you like this article?

Need something else? Request a feature