|
1 | | -import { assert, describe, it } from '@rstest/core'; |
| 1 | +import { assert, describe, it, rs } from '@rstest/core'; |
2 | 2 | import { ModuleFederation } from '../src'; |
3 | 3 | import { getGlobalSnapshot, resetFederationGlobalInfo } from '../src/global'; |
4 | 4 |
|
@@ -46,4 +46,54 @@ describe('snapshot', () => { |
46 | 46 | }, |
47 | 47 | }); |
48 | 48 | }); |
| 49 | + describe('manifest fetch and loadEntryTimeout', () => { |
| 50 | + const Remote1Entry = |
| 51 | + 'http://localhost:1111/resources/snapshot/remote1/federation-manifest.json'; |
| 52 | + |
| 53 | + const withFetchSpy = async ( |
| 54 | + loadEntryTimeout: number | undefined, |
| 55 | + run: (seen: Array<RequestInit | undefined>) => Promise<void>, |
| 56 | + ) => { |
| 57 | + const originalFetch = global.fetch; |
| 58 | + const seen: Array<RequestInit | undefined> = []; |
| 59 | + global.fetch = ((url: RequestInfo | URL, init?: RequestInit) => { |
| 60 | + seen.push(init); |
| 61 | + return originalFetch(url, init); |
| 62 | + }) as typeof fetch; |
| 63 | + try { |
| 64 | + const FM = new ModuleFederation({ |
| 65 | + name: '@snapshot/host-timeout', |
| 66 | + remotes: [{ name: '@snapshot/remote1', entry: Remote1Entry }], |
| 67 | + ...(loadEntryTimeout === undefined ? {} : { loadEntryTimeout }), |
| 68 | + }); |
| 69 | + await FM.loadRemote<() => string>('@snapshot/remote1/say'); |
| 70 | + await run(seen); |
| 71 | + } finally { |
| 72 | + global.fetch = originalFetch; |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + it('bounds the manifest fetch with loadEntryTimeout', async () => { |
| 77 | + const setTimeoutSpy = rs.spyOn(globalThis, 'setTimeout'); |
| 78 | + try { |
| 79 | + await withFetchSpy(4321, async (seen) => { |
| 80 | + expect(seen.length).toBeGreaterThan(0); |
| 81 | + expect(seen[0]?.signal).toBeInstanceOf(AbortSignal); |
| 82 | + expect(setTimeoutSpy).toHaveBeenCalledWith( |
| 83 | + expect.any(Function), |
| 84 | + 4321, |
| 85 | + ); |
| 86 | + }); |
| 87 | + } finally { |
| 88 | + setTimeoutSpy.mockRestore(); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + it('leaves the manifest fetch unbounded without loadEntryTimeout', async () => { |
| 93 | + await withFetchSpy(undefined, async (seen) => { |
| 94 | + expect(seen.length).toBeGreaterThan(0); |
| 95 | + expect(seen[0]?.signal).toBeUndefined(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
49 | 99 | }); |
0 commit comments