|
1 | 1 | # Bi-Directional Hosts Example
|
2 | 2 |
|
3 |
| -This example demos bi-directional hosts each with their own remote `Button` components. |
| 3 | +This example demonstrates bi-directional module federation using Modern.js, where each application can both expose and consume federated modules from each other. |
4 | 4 |
|
5 |
| -- `app1` exposes a red `<button>App 1 Button</button>` component. |
6 |
| -- `app2` exposes a blue `<button>App 2 Button</button>` component. |
| 5 | +- `app1` exposes a red `<button>App 1 Button</button>` component and consumes `app2`'s button. |
| 6 | +- `app2` exposes a blue `<button>App 2 Button</button>` component and consumes `app1`'s button. |
7 | 7 |
|
8 |
| -# Running Demo |
| 8 | +## Technology Stack |
9 | 9 |
|
10 |
| -Run `pnpm run start`. This will build and serve both `app1` and `app2` on ports 3001 and 3002 respectively. |
| 10 | +- **Framework**: Modern.js v2.68.6 |
| 11 | +- **Module Federation**: @module-federation/modern-js v0.17.1 |
| 12 | +- **Testing**: Playwright for end-to-end testing |
| 13 | +- **Build Tool**: Rspack (via Modern.js) |
11 | 14 |
|
12 |
| -- [localhost:3001](http://localhost:3001/) |
13 |
| -- [localhost:3002](http://localhost:3002/) |
| 15 | +## Running Demo |
14 | 16 |
|
15 |
| -Notice that `app1` will asynchronously load `app2`'s button and vice versa. |
16 |
| -<img src="https://ssl.google-analytics.com/collect?v=1&t=event&ec=email&ea=open&t=event&tid=UA-120967034-1&z=1589682154&cid=ae045149-9d17-0367-bbb0-11c41d92b411&dt=ModuleFederationExamples&dp=/email/BiDirectional"> |
| 17 | +Run `pnpm run start`. This will start both `app1` and `app2` in development mode on ports 3001 and 3002 respectively. |
17 | 18 |
|
18 |
| -# Running Cypress E2E Tests |
| 19 | +- [localhost:3001](http://localhost:3001/) - App1 hosting App2's button |
| 20 | +- [localhost:3002](http://localhost:3002/) - App2 hosting App1's button |
19 | 21 |
|
20 |
| -To run tests in interactive mode, run `npm run cypress:debug` from the root directory of the project. It will open Cypress Test Runner and allow to run tests in interactive mode. [More info about "How to run tests"](../../cypress-e2e/README.md#how-to-run-tests) |
| 22 | +Notice that each app will asynchronously load the other app's federated button component using React Suspense. |
21 | 23 |
|
22 |
| -To build app and run test in headless mode, run `yarn e2e:ci`. It will build app and run tests for this workspace in headless mode. If tets failed cypress will create `cypress` directory in sample root folder with screenshots and videos. |
| 24 | +## Available Scripts |
23 | 25 |
|
24 |
| -["Best Practices, Rules amd more interesting information here](../../cypress-e2e/README.md) |
| 26 | +- `pnpm start` - Start both apps in development mode |
| 27 | +- `pnpm build` - Build both apps for production |
| 28 | +- `pnpm serve` - Serve built apps in production mode |
| 29 | +- `pnpm test:e2e` - Run Playwright tests in headless mode |
| 30 | +- `pnpm test:e2e:ui` - Run Playwright tests with UI mode |
| 31 | +- `pnpm test:e2e:debug` - Run Playwright tests in debug mode |
| 32 | +- `pnpm e2e:ci` - Build and run tests for CI |
| 33 | + |
| 34 | +## Module Federation Configuration |
| 35 | + |
| 36 | +Each app uses a `module-federation.config.ts` file to configure: |
| 37 | +- **Exposed modules**: Components shared with other apps |
| 38 | +- **Remote modules**: Components consumed from other apps |
| 39 | +- **Shared dependencies**: React and React-DOM with singleton strategy |
| 40 | + |
| 41 | +## End-to-End Testing with Playwright |
| 42 | + |
| 43 | +This example includes comprehensive Playwright tests that verify: |
| 44 | + |
| 45 | +- ✅ Both applications load correctly |
| 46 | +- ✅ Federated components render properly |
| 47 | +- ✅ Bi-directional communication works |
| 48 | +- ✅ Error handling for module loading failures |
| 49 | +- ✅ Cross-app interaction and state management |
| 50 | + |
| 51 | +### Running Tests |
| 52 | + |
| 53 | +```bash |
| 54 | +# Run tests in headless mode |
| 55 | +pnpm test:e2e |
| 56 | + |
| 57 | +# Run tests with interactive UI |
| 58 | +pnpm test:e2e:ui |
| 59 | + |
| 60 | +# Run tests in debug mode |
| 61 | +pnpm test:e2e:debug |
| 62 | + |
| 63 | +# Run tests for CI (build first) |
| 64 | +pnpm e2e:ci |
| 65 | +``` |
| 66 | + |
| 67 | +### Test Architecture |
| 68 | + |
| 69 | +The test suite includes: |
| 70 | +- **Page Objects**: Reusable test utilities in `e2e/utils/` |
| 71 | +- **Test Fixtures**: Shared setup and teardown logic |
| 72 | +- **Comprehensive Coverage**: All module federation scenarios |
| 73 | +- **Error Scenarios**: Graceful handling of federation failures |
| 74 | + |
| 75 | +## Key Features Demonstrated |
| 76 | + |
| 77 | +1. **Bi-directional Federation**: Apps both expose and consume modules |
| 78 | +2. **React Suspense Integration**: Lazy loading with fallback UI |
| 79 | +3. **Modern.js Framework**: Latest patterns and best practices |
| 80 | +4. **Production-Ready Testing**: Comprehensive Playwright test suite |
| 81 | +5. **TypeScript Support**: Full type safety across federated modules |
0 commit comments