|
| 1 | +// @ts-nocheck |
| 2 | +/* |
| 3 | + * @jest-environment node |
| 4 | + */ |
| 5 | + |
| 6 | +import { vol } from 'memfs'; |
| 7 | +import SharePlugin from '../../../src/lib/sharing/SharePlugin'; |
| 8 | +import { |
| 9 | + createRealCompiler, |
| 10 | + createMemfsCompilation, |
| 11 | + createNormalModuleFactory, |
| 12 | +} from '../../helpers/webpackMocks'; |
| 13 | + |
| 14 | +// Use memfs for fs inside this suite |
| 15 | +jest.mock('fs', () => require('memfs').fs); |
| 16 | +jest.mock('fs/promises', () => require('memfs').fs.promises); |
| 17 | + |
| 18 | +// Mock child plugins to avoid deep integration |
| 19 | +jest.mock('../../../src/lib/sharing/ConsumeSharedPlugin', () => { |
| 20 | + return jest |
| 21 | + .fn() |
| 22 | + .mockImplementation((opts) => ({ options: opts, apply: jest.fn() })); |
| 23 | +}); |
| 24 | +jest.mock('../../../src/lib/sharing/ProvideSharedPlugin', () => { |
| 25 | + return jest |
| 26 | + .fn() |
| 27 | + .mockImplementation((opts) => ({ options: opts, apply: jest.fn() })); |
| 28 | +}); |
| 29 | + |
| 30 | +import ConsumeSharedPlugin from '../../../src/lib/sharing/ConsumeSharedPlugin'; |
| 31 | +import ProvideSharedPlugin from '../../../src/lib/sharing/ProvideSharedPlugin'; |
| 32 | + |
| 33 | +describe('SharePlugin smoke (memfs)', () => { |
| 34 | + beforeEach(() => { |
| 35 | + vol.reset(); |
| 36 | + jest.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('applies child plugins with derived options', () => { |
| 40 | + // Create a tiny project in memfs |
| 41 | + vol.fromJSON({ |
| 42 | + '/test-project/src/index.js': 'console.log("hello")', |
| 43 | + '/test-project/package.json': '{"name":"test","version":"1.0.0"}', |
| 44 | + '/test-project/node_modules/react/index.js': 'module.exports = {}', |
| 45 | + '/test-project/node_modules/lodash/index.js': 'module.exports = {}', |
| 46 | + }); |
| 47 | + |
| 48 | + const plugin = new SharePlugin({ |
| 49 | + shareScope: 'default', |
| 50 | + shared: { |
| 51 | + react: '^17.0.0', |
| 52 | + lodash: { version: '4.17.21', singleton: true }, |
| 53 | + 'components/': { version: '1.0.0', eager: false }, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + const compiler = createRealCompiler('/test-project'); |
| 58 | + expect(() => plugin.apply(compiler as any)).not.toThrow(); |
| 59 | + |
| 60 | + // Child plugins constructed |
| 61 | + expect(ConsumeSharedPlugin).toHaveBeenCalledTimes(1); |
| 62 | + expect(ProvideSharedPlugin).toHaveBeenCalledTimes(1); |
| 63 | + |
| 64 | + // Each child plugin receives shareScope and normalized arrays |
| 65 | + const consumeOpts = (ConsumeSharedPlugin as jest.Mock).mock.calls[0][0]; |
| 66 | + const provideOpts = (ProvideSharedPlugin as jest.Mock).mock.calls[0][0]; |
| 67 | + expect(consumeOpts.shareScope).toBe('default'); |
| 68 | + expect(Array.isArray(consumeOpts.consumes)).toBe(true); |
| 69 | + expect(provideOpts.shareScope).toBe('default'); |
| 70 | + expect(Array.isArray(provideOpts.provides)).toBe(true); |
| 71 | + |
| 72 | + // Simulate compilation lifecycle |
| 73 | + const compilation = createMemfsCompilation(compiler as any); |
| 74 | + const normalModuleFactory = createNormalModuleFactory(); |
| 75 | + expect(() => |
| 76 | + (compiler as any).hooks.thisCompilation.call(compilation, { |
| 77 | + normalModuleFactory, |
| 78 | + }), |
| 79 | + ).not.toThrow(); |
| 80 | + expect(() => |
| 81 | + (compiler as any).hooks.compilation.call(compilation, { |
| 82 | + normalModuleFactory, |
| 83 | + }), |
| 84 | + ).not.toThrow(); |
| 85 | + |
| 86 | + // Child plugin instances should be applied to the compiler |
| 87 | + const consumeInst = (ConsumeSharedPlugin as jest.Mock).mock.results[0] |
| 88 | + .value; |
| 89 | + const provideInst = (ProvideSharedPlugin as jest.Mock).mock.results[0] |
| 90 | + .value; |
| 91 | + expect(consumeInst.apply).toHaveBeenCalledWith(compiler); |
| 92 | + expect(provideInst.apply).toHaveBeenCalledWith(compiler); |
| 93 | + }); |
| 94 | +}); |
0 commit comments