Catalog My learning Blog
Micro-Frontends Course: React, Import Maps and Teams · Phase 4 · Lesson 12 · 5 min read

Testing Micro-Frontends: Contract and End-to-End Tests

On this page
  1. Why testing changes
  2. Unit and component tests: business as usual
  3. Contract tests for the mount function
  4. Contract tests for events
  5. End-to-end tests on the composed page
  6. Testing against what's in production
  7. Check yourself

Why testing changes

In a single frontend, one test suite runs against one build, and if it passes, what you test is what you ship. With micro-frontends, the cart team deploys on Tuesday against whatever the catalog, header and shell happen to be in production that day. No test suite ever saw that exact combination before it went live.

So testing has to answer two separate questions:

  1. Does my micro-frontend work on its own? Each team's ordinary tests.
  2. Does it keep its promises to everyone else? Contract tests, which run in every team's pipeline.

A few end-to-end tests of the composed page then check the most important journeys in a real environment.

Most tests stay inside one team; contracts guard the boundaries; a few end-to-end tests cover key journeys.
Most tests stay inside one team; contracts guard the boundaries; a few end-to-end tests cover key journeys.

Unit and component tests: business as usual

Inside a micro-frontend, test as you would any React app: Vitest or Jest with React Testing Library for components, plain unit tests for logic. Render the component directly, not through mount. Nothing about micro-frontends changes this layer, and it should hold most of each team's tests.

Contract tests for the mount function

The mount contract from Day 4 makes promises: render only inside the given element, accept the props, and clean everything up on unmount. A contract test checks those promises for real, in a browser, using the same import map as production.

The lab runs one harness against every micro-frontend. It loads a blank page with the import map, watches window.addEventListener and removeEventListener, then mounts and unmounts each one:

JavaScript
const el = document.createElement('div');
document.body.append(el);
const mod = await import(app);
const unmount = mod.mount(el, { basePath: '/', currency: 'EUR', navigate: () => {} });
// ...check something rendered, then:
unmount();
// ...check el is empty and every window listener added during mount was removed
Code
@shop/catalog  {"hasMount":true,"unmountIsFn":true,"rendered":true,"emptyAfterUnmount":true,"leakedListeners":[]}
@shop/cart     {"hasMount":true,"unmountIsFn":true,"rendered":true,"emptyAfterUnmount":true,"leakedListeners":["shop:item-added"]}
@shop/header   {"hasMount":true,"unmountIsFn":true,"rendered":true,"emptyAfterUnmount":true,"leakedListeners":[]}
@shop/account  {"hasMount":true,"unmountIsFn":true,"rendered":true,"emptyAfterUnmount":true,"leakedListeners":[]}

The harness caught something. The cart keeps listening for shop:item-added after unmount. In this design that's deliberate, since the cart must hear additions on every page (Day 8). But a test that makes the team say so explicitly, with an allowlist entry like "cart: shop:item-added, started by init", is far better than a leak nobody knows about. Every other listener left behind would be a real memory leak that grows each time a user navigates.

Run the harness in each team's pipeline against their new build plus the current production import map. That's exactly the combination that will be live after their deploy.

Contract tests for events

Events are an API (Day 8), so test both sides of each one:

  • The emitter's side: a component test that clicks "Add" and asserts the shop:item-added event was dispatched with the documented payload.
  • The listener's side: a test that dispatches a documented example payload and asserts the cart reacts.

Keep the example payloads in the contract package, next to the ShopEvents type. Both teams test against the same examples, so a field renamed on one side breaks the other team's build, not production. TypeScript catches most drift already, since both sides import the same types. The shared examples catch the rest, like an emitter that sends quantity: 0.

End-to-end tests on the composed page

A few Playwright tests run the real shell with real micro-frontends in a staging or preview environment. Keep them to the journeys that make money or would make headlines if broken:

JavaScript
test('add to cart from the catalog', async ({ page }) => {
  await page.goto('/');
  await page.getByRole('button', { name: 'Add lamp' }).click();
  await expect(page.locator('#count')).toHaveText('1');
  await page.getByRole('link', { name: /Cart/ }).click();
  await expect(page.getByText('1 x Desk lamp')).toBeVisible();
});

That one test crosses three teams' code and the shell, which is exactly what unit tests can't do. Who owns it? The team whose journey it is, here the cart, with the platform team running the suite.

Add one resilience test too: point the import map at a missing file for one micro-frontend and assert the rest of the page still works. Day 6's lab did exactly that and found the shell's Promise.all was taking down every page.

Testing against what's in production

The import map makes a useful trick possible. Each team's pull request can build a preview map: production's map, with only their own entry replaced by the new build. Run the contract harness and the end-to-end suite against that preview. If it passes, the deploy changes exactly one line from what was tested.

TipKeep a public "storybook" page per micro-frontend that mounts it with fixture props through the real contract. It's documentation for other teams, a manual test bench, and a target for visual regression tests, all at once.

Check yourself

Because after the deploy, the new build will run next to whatever the other teams currently have in production. Testing against that exact combination, production's map with only your own entry replaced, means the deploy changes one line from what was tested.

Each mount adds another listener that's never removed, so memory and work grow as users move around the app, and stale listeners may throw errors against DOM that's gone. The catalog's unmount function must remove it. If a listener is deliberately long-lived, like the cart's, list it in the harness's allowlist with a reason.

The few that cross teams and matter most, like adding to cart and checking out, plus one test that the page survives a micro-frontend failing to load.

Keep example payloads for each event in the contract package. The emitter's tests assert it sends a payload matching the example, and the listener's tests feed it the example. A rename on one side then breaks the other team's tests at build time instead of breaking production.

It proves the shell contains failures, which no ordinary test checks. The course's lab found that a Promise.all at boot made one missing file break every page; a resilience test like this catches that kind of bug before users do.