typescript
// resilience-test.spec.ts
// Playwright test suite for micro-frontend resilience.
// Assumptions:
// - Shell app at http://localhost:3000 (uses Module Federation to mount MFEs).
// - MFE1 remoteEntry.js at http://localhost:3001/remoteEntry.js (e.g., header MFE).
// - MFE2 remoteEntry.js at http://localhost:3002/remoteEntry.js (e.g., dashboard MFE).
// - Shared dependency: React from https://unpkg.com/react@18/umd/react.development.js (federated sharing).
// - MFEs have graceful degradation: ErrorBoundaries, fallback UIs (e.g., "MFE1 unavailable"), lazy-loading.
// - Version skew: MFE1-v1 at :3001 (compatible), MFE1-v2 at :3003 (incompatible props/API, e.g., expects 'color' prop).
// - Run with: npx playwright test --project=chrome
// - Start servers: webpack-dev-server for shell/MFEs beforehand.
import { test, expect, Page } from '@playwright/test';
const SHELL_URL = 'http://localhost:3000';
const MFE1_V1_ENTRY = 'http://localhost:3001/remoteEntry.js';
const MFE1_V2_ENTRY = 'http://localhost:3003/remoteEntry.js';
const MFE2_ENTRY = 'http://localhost:3002/remoteEntry.js';
const SHARED_REACT = 'https://unpkg.com/react@18/umd/react.development.js';
test.describe('Micro-frontend Resilience Tests', () => {
test.beforeEach(async ({ page }) => {
// Ensure clean page state.
await page.goto(SHELL_URL);
await expect(page.locator('#app')).toBeVisible(); // Shell loads.
});
test('injects network partition on MFE1 (graceful fallback)', async ({ page }) => {
// Simulate network partition: block MFE1 remoteEntry load.
await page.route(MFE1_V1_ENTRY, async (route) => {
await route.abort('failed');
});
// Verify shell + MFE2 work, MFE1 shows fallback.
await expect(page.locator('#mfe1-fallback')).toBeVisible({ timeout: 5000 }); // e.g., "Header unavailable"
await expect(page.locator('#mfe2-dashboard')).toContainText('Welcome'); // MFE2 intact.
await expect(page.locator('body')).not.toContainText('Uncaught Error'); // No JS crashes.
});
test('injects network partition on MFE2 (MFE1 intact)', async ({ page }) => {
await page.route(MFE2_ENTRY, async (route) => {
await route.abort('failed');
});
await expect(page.locator('#mfe1-header')).toContainText('Logo'); // MFE1 intact.
await expect(page.locator('#mfe2-fallback')).toBeVisible();
await expect(page.locator('body')).not.toContainText('Uncaught Error');
});
test('injects version skew (MFE1-v2 incompatible with shell)', async ({ page }) => {
// Simulate skew: redirect MFE1-v1 request to incompatible v2 bundle.
await page.route(MFE1_V1_ENTRY, async (route) => {
const v2Response = await page.request.get(MFE1_V2_ENTRY);
await route.fulfill({
response: v2Response,
// Optionally modify headers/content for skew simulation.
});
});
// Verify fallback triggers due to runtime incompatibility (e.g., prop mismatch error caught by ErrorBoundary).
await expect(page.locator('#mfe1-fallback')).toBeVisible({ timeout: 10000 });
await expect(page.locator('#mfe2-dashboard')).toContainText('Welcome'); // Others intact.
// Check console for expected skew error (not crash).
const errors = await page.context().request?.logs() || [];
expect(errors.some(log => log.text.includes('propTypes') || log.text.includes('version mismatch'))).toBe(true);
});
test('injects shared dependency outage (React CDN block)', async ({ page }) => {
// Simulate outage: block federated shared React dep (tests fallback to local bundling).
await page.route(SHARED_REACT, async (route) => {
await route.abort('failed');
});
// Also block any other shared deps if needed.
await page.route('**/lodash*.js', async (route) => route.abort('failed'));
// Verify MFEs fallback to local deps or show partial UI.
await expect(page.locator('#mfe1-fallback-partial')).toBeVisible(); // e.g., Static header.
await expect(page.locator('#mfe2-fallback-partial')).toContainText('Static Dashboard');
await expect(page.locator('body')).not.toContainText('Cannot resolve dependency');
});
test('chaos storm: simultaneous partition + skew + outage', async ({ page }) => {
// Compound failure: all injections at once.
await page.route(MFE1_V1_ENTRY, async (route) => {
// Skew + partition simulation: serve partial v2 then abort chunks.
const v2Response = await page.request.get(MFE1_V2_ENTRY);
await route.fulfill({ response: v2Response });
});
await page.route(MFE2_ENTRY, async (route) => route.abort('failed'));
await page.route(SHARED_REACT, async (route) => route.abort('failed'));
// Verify shell survives, shows fallbacks everywhere.
await expect(page.locator('#shell-fallback')).toBeVisible(); // Ultimate shell fallback.
await expect(page.locator('#mfe1-fallback'), { timeout: 10000 }).toBeVisible();
await expect(page.locator('#mfe2-fallback')).toBeVisible();
await expect(page).not.toHaveTitle(/Error|Crash/); // No page crash.
});
});
// playwright.config.ts snippet for this project:
// module.exports = {
// projects: [{ name: 'chrome', use: { ...devices['Desktop Chrome'] } }],
// use: { baseURL: 'http://localhost:3000' },
// timeout: 30000,
// };
This test injects failures via Playwright's powerful network routing/abort/fulfill, verifying graceful degradation without full outages.