-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
react-module-federation.test.ts
130 lines (111 loc) · 4.11 KB
/
react-module-federation.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { stripIndents } from '@nx/devkit';
import {
checkFilesExist,
cleanupProject,
killPort,
newProject,
readProjectConfig,
runCLI,
runCLIAsync,
uniq,
updateFile,
} from '@nx/e2e/utils';
describe('React Module Federation', () => {
let proj: string;
beforeAll(() => (proj = newProject()));
afterAll(() => cleanupProject());
it('should generate host and remote apps', async () => {
const shell = uniq('shell');
const remote1 = uniq('remote1');
const remote2 = uniq('remote2');
const remote3 = uniq('remote3');
runCLI(`generate @nx/react:host ${shell} --style=css --no-interactive`);
runCLI(
`generate @nx/react:remote ${remote1} --style=css --host=${shell} --no-interactive`
);
runCLI(
`generate @nx/react:remote ${remote2} --style=css --host=${shell} --no-interactive`
);
runCLI(
`generate @nx/react:remote ${remote3} --style=css --host=${shell} --no-interactive`
);
checkFilesExist(`apps/${shell}/module-federation.config.js`);
checkFilesExist(`apps/${remote1}/module-federation.config.js`);
checkFilesExist(`apps/${remote2}/module-federation.config.js`);
checkFilesExist(`apps/${remote3}/module-federation.config.js`);
await expect(runCLIAsync(`test ${shell}`)).resolves.toMatchObject({
combinedOutput: expect.stringContaining('Test Suites: 1 passed, 1 total'),
});
expect(readPort(shell)).toEqual(4200);
expect(readPort(remote1)).toEqual(4201);
expect(readPort(remote2)).toEqual(4202);
expect(readPort(remote3)).toEqual(4203);
updateFile(
`apps/${shell}/webpack.config.js`,
stripIndents`
import { ModuleFederationConfig } from '@nx/devkit';
import { composePlugins, withNx } from '@nx/webpack';
import { withReact } from '@nx/react';
import { withModuleFederation } from '@nx/react/module-federation');
const baseConfig = require('./module-federation.config');
const config: ModuleFederationConfig = {
...baseConfig,
remotes: [
'${remote1}',
['${remote2}', 'http://localhost:${readPort(
remote2
)}/remoteEntry.js'],
['${remote3}', 'http://localhost:${readPort(remote3)}'],
],
};
// Nx plugins for webpack to build config object from Nx options and context.
module.exports = composePlugins(withNx(), withReact(), withModuleFederation(config));
`
);
updateFile(
`apps/${shell}-e2e/src/integration/app.spec.ts`,
stripIndents`
import { getGreeting } from '../support/app.po';
describe('shell app', () => {
it('should display welcome message', () => {
cy.visit('/')
getGreeting().contains('Welcome ${shell}');
});
it('should load remote 1', () => {
cy.visit('/${remote1}')
getGreeting().contains('Welcome ${remote1}');
});
it('should load remote 2', () => {
cy.visit('/${remote2}')
getGreeting().contains('Welcome ${remote2}');
});
it('should load remote 3', () => {
cy.visit('/${remote3}')
getGreeting().contains('Welcome ${remote3}');
});
});
`
);
// TODO(caleb): cypress isn't able to find the element and then throws error with an address already in use error.
// https://staging.nx.app/runs/ASAokpXhnE/task/e2e-react:e2e
// if (runCypressTests()) {
// const e2eResults = runCLI(`e2e ${shell}-e2e --no-watch --verbose`);
// expect(e2eResults).toContain('All specs passed!');
// expect(
// await killPorts([
// readPort(shell),
// readPort(remote1),
// readPort(remote2),
// readPort(remote3),
// ])
// ).toBeTruthy();
// }
}, 500_000);
async function readPort(appName: string): Promise<number> {
const config = await readProjectConfig(appName);
return config.targets.serve.options.port;
}
});
function killPorts(ports: number[]): Promise<boolean[]> {
return Promise.all(ports.map((p) => killPort(p)));
}