-
-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathregister-remotes.spec.ts
More file actions
346 lines (332 loc) · 10.3 KB
/
Copy pathregister-remotes.spec.ts
File metadata and controls
346 lines (332 loc) · 10.3 KB
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
338
339
340
341
342
343
344
345
346
import { assert, describe, it, expect, rs } from '@rstest/core';
import {
ModuleFederation,
CurrentGlobal,
setGlobalFederationInstance,
} from '../src/index';
describe('ModuleFederation', () => {
it('registers new remotes and loads them correctly', async () => {
const FM = new ModuleFederation({
name: '@federation/instance',
version: '1.0.1',
remotes: [
{
name: '@register-remotes/app1',
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry.js',
},
],
});
const app1Module = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/app1/say',
);
assert(app1Module);
const app1Res = await app1Module();
expect(app1Res).toBe('hello app1 entry1');
// Register new remotes
FM.registerRemotes([
{
name: '@register-remotes/app2',
entry:
'http://localhost:1111/resources/register-remotes/app2/federation-remote-entry.js',
},
]);
const app2Module = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/app2/say',
);
assert(app2Module);
const res = await app2Module();
expect(res).toBe('hello app2');
});
it('does not merge loaded remote by default', async () => {
const FM = new ModuleFederation({
name: '@federation/instance',
version: '1.0.1',
remotes: [
{
name: '@register-remotes/app1',
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry.js',
},
],
});
FM.registerRemotes([
{
name: '@register-remotes/app1',
// Entry is different from the registered remote
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry2.js',
},
]);
const app1Module = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/app1/say',
);
assert(app1Module);
const app1Res = await app1Module();
expect(app1Res).toBe('hello app1 entry1');
});
it('merges loaded remote by setting "force: true"', async () => {
const FM = new ModuleFederation({
name: '@federation/instance',
version: '1.0.1',
remotes: [
{
name: '@register-remotes/app1',
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry.js',
},
],
});
const app1Module = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/app1/say',
);
assert(app1Module);
const app1Res = await app1Module();
expect(app1Res).toBe('hello app1 entry1');
FM.registerRemotes(
[
{
name: '@register-remotes/app1',
// Entry is different from the registered remote
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry2.js',
},
],
{ force: true },
);
const newApp1Module = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/app1/say',
);
assert(newApp1Module);
const newApp1Res = await newApp1Module();
// Value is different from the registered remote
expect(newApp1Res).toBe('hello app1 entry2');
});
it('removes the remote runtime instance matched by entryGlobalName when force registering', async () => {
const buildName = '@register-remotes/app1';
const registeredName = '@register-remotes/app1-alias';
// Drop any container global left behind by earlier tests so the entry
// script is executed again instead of being reused.
delete (CurrentGlobal as Record<string, unknown>)[buildName];
const FM = new ModuleFederation({
name: '@federation/instance',
version: '1.0.1',
remotes: [
{
// Registered name differs from the name the remote was built with
name: registeredName,
entryGlobalName: buildName,
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry.js',
},
],
});
const app1Module = await FM.loadRemote<Promise<() => string>>(
`${registeredName}/say`,
);
assert(app1Module);
expect(await app1Module()).toBe('hello app1 entry1');
// Simulate the remote's own runtime instance, named after its build name
const remoteInstance = new ModuleFederation({
name: buildName,
remotes: [],
});
setGlobalFederationInstance(remoteInstance);
expect(CurrentGlobal.__FEDERATION__.__INSTANCES__).toContain(
remoteInstance,
);
const warnSpy = rs.spyOn(console, 'warn').mockImplementation(() => {});
try {
FM.registerRemotes(
[
{
name: registeredName,
entryGlobalName: buildName,
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry2.js',
},
],
{ force: true },
);
expect(CurrentGlobal.__FEDERATION__.__INSTANCES__).not.toContain(
remoteInstance,
);
expect(FM.moduleCache.has(registeredName)).toBe(false);
expect(
warnSpy.mock.calls.some((call) =>
call.some(
(arg) =>
typeof arg === 'string' &&
arg.includes('__FEDERATION__.__INSTANCES__'),
),
),
).toBe(false);
} finally {
warnSpy.mockRestore();
}
const newApp1Module = await FM.loadRemote<Promise<() => string>>(
`${registeredName}/say`,
);
assert(newApp1Module);
expect(await newApp1Module()).toBe('hello app1 entry2');
});
it('warns and keeps __INSTANCES__ unchanged when no runtime instance matches the removed remote', async () => {
delete (CurrentGlobal as Record<string, unknown>)['@register-remotes/app1'];
const FM = new ModuleFederation({
name: '@federation/instance',
version: '1.0.1',
remotes: [
{
name: '@register-remotes/app1',
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry.js',
},
],
});
const app1Module = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/app1/say',
);
assert(app1Module);
expect(await app1Module()).toBe('hello app1 entry1');
const unrelatedInstance = new ModuleFederation({
name: '@register-remotes/unrelated',
remotes: [],
});
setGlobalFederationInstance(unrelatedInstance);
const instancesBefore = [...CurrentGlobal.__FEDERATION__.__INSTANCES__];
const warnSpy = rs.spyOn(console, 'warn').mockImplementation(() => {});
try {
FM.registerRemotes(
[
{
name: '@register-remotes/app1',
entry:
'http://localhost:1111/resources/register-remotes/app1/federation-remote-entry2.js',
},
],
{ force: true },
);
expect(CurrentGlobal.__FEDERATION__.__INSTANCES__).toEqual(
instancesBefore,
);
const warning = warnSpy.mock.calls
.flat()
.find(
(arg) =>
typeof arg === 'string' &&
arg.includes('__FEDERATION__.__INSTANCES__'),
);
expect(warning).toBeDefined();
expect(warning).toContain('"@register-remotes/app1"');
expect(warning).toContain(
'share scope and instance could not be released',
);
} finally {
warnSpy.mockRestore();
}
});
it('reloads manifest snapshots when a manifest remote is force registered with the same entry', async () => {
const manifestUrl =
'http://localhost:1111/resources/register-remotes/manifest/federation-manifest.json';
const manifests = [
{
id: '@register-remotes/manifest',
name: '@register-remotes/manifest',
metaData: {
name: '@register-remotes/manifest',
publicPath: 'http://localhost:1111/',
type: 'app',
globalName: '@snapshot/remote1',
buildInfo: {
buildVersion: 'first',
},
remoteEntry: {
name: 'federation-remote-entry.js',
path: 'resources/snapshot/remote1',
},
types: {
name: 'index.d.ts',
path: './',
},
},
remotes: [],
shared: [],
exposes: [],
},
{
id: '@register-remotes/manifest',
name: '@register-remotes/manifest',
metaData: {
name: '@register-remotes/manifest',
publicPath: 'http://localhost:1111/',
type: 'app',
globalName: '@snapshot/remote2',
buildInfo: {
buildVersion: 'second',
},
remoteEntry: {
name: 'federation-remote-entry.js',
path: 'resources/snapshot/remote2',
},
types: {
name: 'index.d.ts',
path: './',
},
},
remotes: [],
shared: [],
exposes: [],
},
];
const manifestFetch = rs.fn((url: string) => {
if (url === manifestUrl) {
return Promise.resolve(
new Response(
JSON.stringify(manifests[manifestFetch.mock.calls.length - 1]),
{
status: 200,
statusText: 'OK',
headers: { 'Content-Type': 'application/json' },
},
),
);
}
});
const FM = new ModuleFederation({
name: '@federation/instance',
version: '1.0.1',
remotes: [
{
name: '@register-remotes/manifest',
entry: manifestUrl,
},
],
plugins: [
{
name: 'manifest-fetch',
fetch: manifestFetch,
},
],
});
const appModule = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/manifest/say',
);
assert(appModule);
expect(await appModule()).toBe('hello world "@snapshot/remote1"');
FM.registerRemotes(
[
{
name: '@register-remotes/manifest',
entry: manifestUrl,
},
],
{ force: true },
);
const nextAppModule = await FM.loadRemote<Promise<() => string>>(
'@register-remotes/manifest/say',
);
assert(nextAppModule);
expect(await nextAppModule()).toBe('hello world "@snapshot/remote2"');
expect(manifestFetch).toHaveBeenCalledTimes(2);
});
});