|
| 1 | +/** |
| 2 | + * Module Federation Mock Utilities for Cypress Tests |
| 3 | + * |
| 4 | + * This file provides utilities to mock module federation behavior |
| 5 | + * during Cypress tests to prevent runtime errors. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Mock remote entry content that provides a basic module federation interface |
| 10 | + */ |
| 11 | +export const createMockRemoteEntry = (moduleName: string): string => ` |
| 12 | + // Mock module federation remote entry for ${moduleName} |
| 13 | + (function() { |
| 14 | + if (!window.__FEDERATION__) { |
| 15 | + window.__FEDERATION__ = {}; |
| 16 | + } |
| 17 | + |
| 18 | + if (!window.__FEDERATION__.moduleMap) { |
| 19 | + window.__FEDERATION__.moduleMap = {}; |
| 20 | + } |
| 21 | + |
| 22 | + // Mock the module exports |
| 23 | + window.__FEDERATION__.moduleMap['${moduleName}'] = { |
| 24 | + get: function(scope) { |
| 25 | + if (scope === 'extensions') { |
| 26 | + return Promise.resolve({ |
| 27 | + default: [] |
| 28 | + }); |
| 29 | + } |
| 30 | + return Promise.resolve({}); |
| 31 | + }, |
| 32 | + init: function() { |
| 33 | + return Promise.resolve(); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + // Export for ES modules compatibility |
| 38 | + if (typeof module !== 'undefined' && module.exports) { |
| 39 | + module.exports = window.__FEDERATION__.moduleMap['${moduleName}']; |
| 40 | + } |
| 41 | + })(); |
| 42 | +`; |
| 43 | + |
| 44 | +/** |
| 45 | + * Cypress command to set up module federation mocks |
| 46 | + */ |
| 47 | +export const setupModuleFederationMocks = (modules: string[] = ['modelRegistry']): void => { |
| 48 | + modules.forEach((moduleName) => { |
| 49 | + // Mock the remote entry file |
| 50 | + cy.intercept( |
| 51 | + { pathname: `/_mf/${moduleName}/remoteEntry.js` }, |
| 52 | + { |
| 53 | + statusCode: 200, |
| 54 | + headers: { 'content-type': 'application/javascript' }, |
| 55 | + body: createMockRemoteEntry(moduleName), |
| 56 | + }, |
| 57 | + ); |
| 58 | + |
| 59 | + // Mock the extensions endpoint |
| 60 | + cy.intercept( |
| 61 | + { url: `**/_mf/${moduleName}/**/extensions` }, |
| 62 | + { |
| 63 | + statusCode: 200, |
| 64 | + headers: { 'content-type': 'application/javascript' }, |
| 65 | + body: 'export default [];', |
| 66 | + }, |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + // Mock any other module federation paths with 404 |
| 71 | + cy.intercept({ pathname: '/_mf/**' }, { statusCode: 404 }); |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * Add the command to Cypress |
| 76 | + */ |
| 77 | +// eslint-disable-next-line @typescript-eslint/no-namespace |
| 78 | +declare global { |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-namespace |
| 80 | + namespace Cypress { |
| 81 | + interface Chainable { |
| 82 | + setupModuleFederationMocks: (modules?: string[]) => Chainable<void>; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +Cypress.Commands.add('setupModuleFederationMocks', setupModuleFederationMocks); |
0 commit comments