|
1 | | -import defu from 'defu'; |
2 | 1 | import { resolve } from 'path'; |
3 | | -import { build, Rollup } from 'vite'; |
4 | 2 | import { describe, expect, it } from 'vitest'; |
5 | | -import { federation } from '../src/index'; |
6 | | -import { findAsset, findChunk, getChunkNames } from './helpers/matchers'; |
| 3 | +import { buildFixture, FIXTURES } from './helpers/build'; |
| 4 | +import { findAsset, findChunk, getAllChunkCode, getChunkNames } from './helpers/matchers'; |
7 | 5 |
|
8 | | -const FIXTURES = resolve(__dirname, 'fixtures'); |
9 | | -const BASIC_REMOTE = resolve(FIXTURES, 'basic-remote'); |
10 | | - |
11 | | -async function buildFixture(optionsOverrides?: Partial<Parameters<typeof federation>[0]>) { |
12 | | - const defaultOptions = { |
13 | | - name: 'basicRemote', |
14 | | - filename: 'remoteEntry.js', |
15 | | - exposes: { |
16 | | - './exposed': resolve(BASIC_REMOTE, 'exposed-module.js'), |
17 | | - }, |
18 | | - shared: {}, |
19 | | - dts: false, |
20 | | - } satisfies Parameters<typeof federation>[0]; |
21 | | - |
22 | | - const mfOptions = defu(defaultOptions, optionsOverrides); |
23 | | - |
24 | | - const result = await build({ |
25 | | - root: BASIC_REMOTE, |
26 | | - logLevel: 'silent', |
27 | | - plugins: [federation(mfOptions)], |
28 | | - build: { |
29 | | - write: false, |
30 | | - target: 'chrome89', |
31 | | - }, |
32 | | - }); |
33 | | - |
34 | | - // Vite returns RollupOutput[] only with multiple rollupOptions.output entries. |
35 | | - // Our test configs should never produce that — fail fast if they do. |
36 | | - expect(Array.isArray(result), 'E xpected a single RollupOutput, not an array').toBe(false); |
37 | | - return result as Rollup.RollupOutput; |
38 | | -} |
| 6 | +const BASIC_REMOTE_MF_OPTIONS = { |
| 7 | + exposes: { |
| 8 | + './exposed': resolve(FIXTURES, 'basic-remote', 'exposed-module.js'), |
| 9 | + }, |
| 10 | +}; |
39 | 11 |
|
40 | 12 | describe('build', () => { |
41 | 13 | describe('remote', () => { |
42 | 14 | it('produces a remoteEntry chunk', async () => { |
43 | | - const output = await buildFixture(); |
| 15 | + const output = await buildFixture({ mfOptions: BASIC_REMOTE_MF_OPTIONS }); |
44 | 16 | const chunks = getChunkNames(output); |
45 | 17 | expect(chunks.some((name) => name.includes('remoteEntry'))).toBe(true); |
46 | 18 | }); |
47 | 19 |
|
48 | 20 | it('remoteEntry contains federation runtime init with correct name', async () => { |
49 | | - const output = await buildFixture(); |
| 21 | + const output = await buildFixture({ mfOptions: BASIC_REMOTE_MF_OPTIONS }); |
50 | 22 | const remoteEntry = findChunk(output, 'remoteEntry'); |
51 | 23 | expect(remoteEntry).toBeDefined(); |
52 | 24 | expect(remoteEntry!.code).toContain('basicRemote'); |
53 | 25 | expect(remoteEntry!.code).toContain('moduleCache'); |
54 | 26 | }); |
55 | 27 |
|
56 | 28 | it('exposed module content is included in output', async () => { |
57 | | - const output = await buildFixture(); |
58 | | - const allCode = output.output |
59 | | - .filter((o): o is Rollup.OutputChunk => o.type === 'chunk') |
60 | | - .map((c) => c.code) |
61 | | - .join('\n'); |
62 | | - |
| 29 | + const output = await buildFixture({ mfOptions: BASIC_REMOTE_MF_OPTIONS }); |
| 30 | + const allCode = getAllChunkCode(output); |
63 | 31 | expect(allCode).toContain('Hello'); |
64 | 32 | }); |
65 | 33 |
|
66 | 34 | it('generates mf-manifest.json when manifest is enabled', async () => { |
67 | 35 | const manifestOutput = await buildFixture({ |
68 | | - manifest: true, |
| 36 | + mfOptions: { ...BASIC_REMOTE_MF_OPTIONS, manifest: true }, |
69 | 37 | }); |
70 | 38 |
|
71 | 39 | const manifest = findAsset(manifestOutput, 'mf-manifest.json'); |
|
0 commit comments