|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { ConfigError } from '../../src/utils/errors'; |
| 3 | + |
| 4 | +vi.mock('../../src/utils/metro-compat', () => { |
| 5 | + const baseJSBundle = vi.fn(() => ({ mocked: true })); |
| 6 | + |
| 7 | + return { |
| 8 | + CountingSet: class CountingSet<T> extends Set<T> {}, |
| 9 | + baseJSBundle, |
| 10 | + bundleToString: vi.fn(() => ({ code: 'serialized-output' })), |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +import { getModuleFederationSerializer } from '../../src/plugin/serializer'; |
| 15 | +import { baseJSBundle } from '../../src/utils/metro-compat'; |
| 16 | + |
| 17 | +function createSerializer(exposes: Record<string, string>) { |
| 18 | + return getModuleFederationSerializer( |
| 19 | + { |
| 20 | + name: 'MFExampleMini', |
| 21 | + filename: 'mini.bundle', |
| 22 | + remotes: {}, |
| 23 | + exposes, |
| 24 | + shared: {}, |
| 25 | + shareStrategy: 'loaded-first', |
| 26 | + plugins: [], |
| 27 | + }, |
| 28 | + true, |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +function createSerializerOptions(projectRoot = '/projectRoot') { |
| 33 | + return { |
| 34 | + runModule: false, |
| 35 | + modulesOnly: true, |
| 36 | + projectRoot, |
| 37 | + } as any; |
| 38 | +} |
| 39 | + |
| 40 | +function createGraph() { |
| 41 | + return { |
| 42 | + dependencies: new Map(), |
| 43 | + } as any; |
| 44 | +} |
| 45 | + |
| 46 | +describe('getModuleFederationSerializer', () => { |
| 47 | + beforeEach(() => { |
| 48 | + vi.clearAllMocks(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('matches expose paths when the entry path contains backslashes', async () => { |
| 52 | + const serializer = createSerializer({ './info': './src/info.tsx' }); |
| 53 | + |
| 54 | + await expect( |
| 55 | + serializer( |
| 56 | + '/projectRoot/src\\info.tsx', |
| 57 | + [], |
| 58 | + createGraph(), |
| 59 | + createSerializerOptions(), |
| 60 | + ), |
| 61 | + ).resolves.toBe('serialized-output'); |
| 62 | + expect(baseJSBundle).toHaveBeenCalledTimes(1); |
| 63 | + }); |
| 64 | + |
| 65 | + it('matches expose paths without extension against resolved entry files', async () => { |
| 66 | + const serializer = createSerializer({ './info': './src/info' }); |
| 67 | + |
| 68 | + await expect( |
| 69 | + serializer( |
| 70 | + '/projectRoot/src/info.tsx', |
| 71 | + [], |
| 72 | + createGraph(), |
| 73 | + createSerializerOptions(), |
| 74 | + ), |
| 75 | + ).resolves.toBe('serialized-output'); |
| 76 | + expect(baseJSBundle).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('prefers exact expose path match over extensionless fallback', async () => { |
| 80 | + const serializer = createSerializer({ |
| 81 | + './js': './src/info.js', |
| 82 | + './tsx': './src/info.tsx', |
| 83 | + }); |
| 84 | + |
| 85 | + await expect( |
| 86 | + serializer( |
| 87 | + '/projectRoot/src/info.tsx', |
| 88 | + [], |
| 89 | + createGraph(), |
| 90 | + createSerializerOptions(), |
| 91 | + ), |
| 92 | + ).resolves.toBe('serialized-output'); |
| 93 | + expect(baseJSBundle).toHaveBeenCalledTimes(1); |
| 94 | + |
| 95 | + const preModules = vi.mocked(baseJSBundle).mock.calls[0][1] as any[]; |
| 96 | + expect(preModules[0].output[0].data.code).toContain('["exposed/tsx"]'); |
| 97 | + expect(preModules[1].output[0].data.code).toContain('["exposed/tsx"]'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('throws a config error when no expose entry matches', async () => { |
| 101 | + const serializer = createSerializer({ './other': './src/other.tsx' }); |
| 102 | + |
| 103 | + await expect( |
| 104 | + serializer( |
| 105 | + '/projectRoot/src/info.tsx', |
| 106 | + [], |
| 107 | + createGraph(), |
| 108 | + createSerializerOptions(), |
| 109 | + ), |
| 110 | + ).rejects.toBeInstanceOf(ConfigError); |
| 111 | + }); |
| 112 | +}); |
0 commit comments