|
| 1 | +import defu from 'defu'; |
| 2 | +import { resolve } from 'path'; |
| 3 | +import { build, Rollup } from 'vite'; |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | +import { federation } from '../src/index'; |
| 6 | +import { findAsset, findChunk, getChunkNames } from './helpers/matchers'; |
| 7 | + |
| 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 | +} |
| 39 | + |
| 40 | +describe('build', () => { |
| 41 | + describe('remote', () => { |
| 42 | + it('produces a remoteEntry chunk', async () => { |
| 43 | + const output = await buildFixture(); |
| 44 | + const chunks = getChunkNames(output); |
| 45 | + expect(chunks.some((name) => name.includes('remoteEntry'))).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('remoteEntry contains federation runtime init with correct name', async () => { |
| 49 | + const output = await buildFixture(); |
| 50 | + const remoteEntry = findChunk(output, 'remoteEntry'); |
| 51 | + expect(remoteEntry).toBeDefined(); |
| 52 | + expect(remoteEntry!.code).toContain('basicRemote'); |
| 53 | + expect(remoteEntry!.code).toContain('moduleCache'); |
| 54 | + }); |
| 55 | + |
| 56 | + 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 | + |
| 63 | + expect(allCode).toContain('Hello'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('generates mf-manifest.json when manifest is enabled', async () => { |
| 67 | + const manifestOutput = await buildFixture({ |
| 68 | + manifest: true, |
| 69 | + }); |
| 70 | + |
| 71 | + const manifest = findAsset(manifestOutput, 'mf-manifest.json'); |
| 72 | + expect(manifest).toBeDefined(); |
| 73 | + |
| 74 | + const parsed = JSON.parse(manifest!.source as string); |
| 75 | + expect(parsed).toHaveProperty('exposes'); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments