/
setup-mfe.spec.ts
337 lines (290 loc) · 8.44 KB
/
setup-mfe.spec.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { ProjectConfiguration, readJson, Tree } from '@nrwl/devkit';
import { readProjectConfiguration } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { setupMfe } from './setup-mfe';
import applicationGenerator from '../application/application';
describe('Init MFE', () => {
let tree: Tree;
beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
await applicationGenerator(tree, {
name: 'app1',
routing: true,
});
await applicationGenerator(tree, {
name: 'remote1',
routing: true,
});
});
test.each([
['app1', 'host'],
['remote1', 'remote'],
])(
'should create webpack and mfe configs correctly',
async (app, type: 'host' | 'remote') => {
// ACT
await setupMfe(tree, {
appName: app,
mfeType: type,
});
// ASSERT
expect(tree.exists(`apps/${app}/mfe.config.js`)).toBeTruthy();
expect(tree.exists(`apps/${app}/webpack.config.js`)).toBeTruthy();
expect(tree.exists(`apps/${app}/webpack.prod.config.js`)).toBeTruthy();
const webpackContents = tree.read(
`apps/${app}/webpack.config.js`,
'utf-8'
);
expect(webpackContents).toMatchSnapshot();
const mfeConfigContents = tree.read(`apps/${app}/mfe.config.js`, 'utf-8');
expect(mfeConfigContents).toMatchSnapshot();
}
);
test.each([
['app1', 'host'],
['remote1', 'remote'],
])(
'create bootstrap file with the contents of main.ts',
async (app, type: 'host' | 'remote') => {
// ARRANGE
const mainContents = tree.read(`apps/${app}/src/main.ts`, 'utf-8');
// ACT
await setupMfe(tree, {
appName: app,
mfeType: type,
});
// ASSERT
const bootstrapContents = tree.read(
`apps/${app}/src/bootstrap.ts`,
'utf-8'
);
const updatedMainContents = tree.read(`apps/${app}/src/main.ts`, 'utf-8');
expect(bootstrapContents).toEqual(mainContents);
expect(updatedMainContents).not.toEqual(mainContents);
}
);
test.each([
['app1', 'host'],
['remote1', 'remote'],
])(
'should alter main.ts to import the bootstrap file dynamically',
async (app, type: 'host' | 'remote') => {
// ARRANGE
const mainContents = tree.read(`apps/${app}/src/main.ts`, 'utf-8');
// ACT
await setupMfe(tree, {
appName: app,
mfeType: type,
});
// ASSERT
const updatedMainContents = tree.read(`apps/${app}/src/main.ts`, 'utf-8');
expect(updatedMainContents).toEqual(
`import('./bootstrap').catch(err => console.error(err))`
);
expect(updatedMainContents).not.toEqual(mainContents);
}
);
test.each([
['app1', 'host'],
['remote1', 'remote'],
])(
'should change the build and serve target and set correct path to webpack config',
async (app, type: 'host' | 'remote') => {
// ACT
await setupMfe(tree, {
appName: app,
mfeType: type,
});
// ASSERT
const { build, serve } = readProjectConfiguration(tree, app).targets;
expect(serve.executor).toEqual('@nrwl/angular:webpack-server');
expect(build.executor).toEqual('@nrwl/angular:webpack-browser');
expect(build.options.customWebpackConfig.path).toEqual(
`apps/${app}/webpack.config.js`
);
}
);
it('should add the remote config to the host when --remotes flag supplied', async () => {
// ACT
await setupMfe(tree, {
appName: 'app1',
mfeType: 'host',
remotes: ['remote1'],
});
// ASSERT
const mfeConfigContents = tree.read(`apps/app1/mfe.config.js`, 'utf-8');
expect(mfeConfigContents).toContain(`'remote1'`);
});
it('should add a remote application and add it to a specified host applications webpack config when no other remote has been added to it', async () => {
// ARRANGE
await setupMfe(tree, {
appName: 'app1',
mfeType: 'host',
});
// ACT
await setupMfe(tree, {
appName: 'remote1',
mfeType: 'remote',
host: 'app1',
});
// ASSERT
const hostMfeConfig = tree.read('apps/app1/mfe.config.js', 'utf-8');
expect(hostMfeConfig).toMatchSnapshot();
});
it('should add a remote application and add it to a specified host applications webpack config that contains a remote application already', async () => {
// ARRANGE
await applicationGenerator(tree, {
name: 'remote2',
});
await setupMfe(tree, {
appName: 'app1',
mfeType: 'host',
});
await setupMfe(tree, {
appName: 'remote1',
mfeType: 'remote',
host: 'app1',
port: 4201,
});
// ACT
await setupMfe(tree, {
appName: 'remote2',
mfeType: 'remote',
host: 'app1',
port: 4202,
});
// ASSERT
const hostMfeConfig = tree.read('apps/app1/mfe.config.js', 'utf-8');
expect(hostMfeConfig).toMatchSnapshot();
});
it('should add a remote application and add it to a specified host applications router config', async () => {
// ARRANGE
await applicationGenerator(tree, {
name: 'remote2',
routing: true,
});
await setupMfe(tree, {
appName: 'app1',
mfeType: 'host',
routing: true,
});
await setupMfe(tree, {
appName: 'remote1',
mfeType: 'remote',
host: 'app1',
port: 4201,
routing: true,
});
// ACT
await setupMfe(tree, {
appName: 'remote2',
mfeType: 'remote',
host: 'app1',
port: 4202,
routing: true,
});
// ASSERT
const hostAppModule = tree.read('apps/app1/src/app/app.module.ts', 'utf-8');
expect(hostAppModule).toMatchSnapshot();
});
it('should add a remote application and add it to a specified host applications serve-mfe target', async () => {
// ARRANGE
await applicationGenerator(tree, {
name: 'remote2',
routing: true,
});
await setupMfe(tree, {
appName: 'app1',
mfeType: 'host',
routing: true,
});
await setupMfe(tree, {
appName: 'remote1',
mfeType: 'remote',
host: 'app1',
port: 4201,
routing: true,
});
// ACT
await setupMfe(tree, {
appName: 'remote2',
mfeType: 'remote',
host: 'app1',
port: 4202,
routing: true,
});
// ASSERT
const hostAppConfig = readProjectConfiguration(tree, 'app1');
const serveMfe = hostAppConfig.targets['serve-mfe'];
expect(serveMfe.options.commands).toContain('nx serve remote1');
expect(serveMfe.options.commands).toContain('nx serve remote2');
expect(serveMfe.options.commands).toContain('nx serve app1');
});
it('should modify the associated cypress project to add the workaround correctly', async () => {
// ARRANGE
await applicationGenerator(tree, {
name: 'testApp',
routing: true,
});
// ACT
await setupMfe(tree, {
appName: 'test-app',
mfeType: 'host',
routing: true,
});
// ASSERT
const cypressCommands = tree.read(
'apps/test-app-e2e/src/support/index.ts',
'utf-8'
);
expect(cypressCommands).toContain(
"Cannot use 'import.meta' outside a module"
);
});
describe('--federationType=dynamic', () => {
it('should create a host with the correct configurations', async () => {
// ARRANGE & ACT
await setupMfe(tree, {
appName: 'app1',
mfeType: 'host',
routing: true,
federationType: 'dynamic',
});
// ASSERT
expect(tree.read('apps/app1/mfe.config.js', 'utf-8')).toContain(
'remotes: []'
);
expect(
tree.exists('apps/app1/src/assets/mfe.manifest.json')
).toBeTruthy();
expect(tree.read('apps/app1/src/main.ts', 'utf-8')).toMatchSnapshot();
});
});
it('should add a remote to dynamic host correctly', async () => {
// ARRANGE
await setupMfe(tree, {
appName: 'app1',
mfeType: 'host',
routing: true,
federationType: 'dynamic',
});
// ACT
await setupMfe(tree, {
appName: 'remote1',
mfeType: 'remote',
port: 4201,
host: 'app1',
routing: true,
});
// ASSERT
expect(tree.read('apps/app1/mfe.config.js', 'utf-8')).toContain(
'remotes: []'
);
expect(readJson(tree, 'apps/app1/src/assets/mfe.manifest.json')).toEqual({
remote1: 'http://localhost:4201',
});
expect(
tree.read('apps/app1/src/app/app.module.ts', 'utf-8')
).toMatchSnapshot();
});
});