|
| 1 | +/** |
| 2 | + * Test to reproduce the Maximum call stack size exceeded error |
| 3 | + * when there are circular dependencies in module loading |
| 4 | + */ |
| 5 | + |
| 6 | +import { jest } from '@jest/globals'; |
| 7 | + |
| 8 | +// Mock vm and fetch to simulate the error scenario |
| 9 | +const mockVm = { |
| 10 | + Script: jest.fn(), |
| 11 | + SourceTextModule: jest.fn(), |
| 12 | + constants: { |
| 13 | + USE_MAIN_CONTEXT_DEFAULT_LOADER: undefined, |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +const mockFetch = jest.fn(); |
| 18 | + |
| 19 | +// Mock the modules that would be imported |
| 20 | +jest.mock('vm', () => mockVm, { virtual: true }); |
| 21 | +jest.mock( |
| 22 | + 'path', |
| 23 | + () => ({ |
| 24 | + basename: jest.fn(), |
| 25 | + join: jest.fn(), |
| 26 | + }), |
| 27 | + { virtual: true }, |
| 28 | +); |
| 29 | + |
| 30 | +describe('Node importNodeModule recursion test', () => { |
| 31 | + beforeEach(() => { |
| 32 | + jest.clearAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should reproduce maximum call stack size exceeded', async () => { |
| 36 | + // Mock a scenario where importNodeModule gets called recursively |
| 37 | + let callCount = 0; |
| 38 | + const maxCalls = 10000; // Set high to trigger stack overflow |
| 39 | + |
| 40 | + // Create a mock script that will trigger recursive importModuleDynamically calls |
| 41 | + const mockScript = { |
| 42 | + runInThisContext: jest.fn(() => { |
| 43 | + return ( |
| 44 | + exports: any, |
| 45 | + module: any, |
| 46 | + require: any, |
| 47 | + dirname: string, |
| 48 | + filename: string, |
| 49 | + ) => { |
| 50 | + // Simulate module execution that triggers another import |
| 51 | + module.exports = {}; |
| 52 | + }; |
| 53 | + }), |
| 54 | + }; |
| 55 | + |
| 56 | + // Set up the vm.Script mock to simulate importModuleDynamically calls |
| 57 | + mockVm.Script.mockImplementation((code: string, options: any) => { |
| 58 | + // Simulate the importModuleDynamically option being called |
| 59 | + if (options.importModuleDynamically && callCount < maxCalls) { |
| 60 | + callCount++; |
| 61 | + // This simulates a circular dependency where modules import each other |
| 62 | + setTimeout(() => { |
| 63 | + options.importModuleDynamically('circular-module').catch(() => {}); |
| 64 | + }, 0); |
| 65 | + } |
| 66 | + return mockScript; |
| 67 | + }); |
| 68 | + |
| 69 | + // Mock fetch to return some JavaScript code |
| 70 | + mockFetch.mockResolvedValue({ |
| 71 | + text: () => Promise.resolve('module.exports = {};'), |
| 72 | + }); |
| 73 | + |
| 74 | + // Simulate the createScriptNode function call that leads to recursion |
| 75 | + const { createScriptNode } = await import('../node'); |
| 76 | + |
| 77 | + // This should trigger the recursive calls and eventually cause a stack overflow |
| 78 | + const promise = new Promise((resolve, reject) => { |
| 79 | + createScriptNode( |
| 80 | + 'http://example.com/test.js', |
| 81 | + (error, scriptContext) => { |
| 82 | + if (error) { |
| 83 | + reject(error); |
| 84 | + } else { |
| 85 | + resolve(scriptContext); |
| 86 | + } |
| 87 | + }, |
| 88 | + {}, |
| 89 | + { |
| 90 | + fetch: mockFetch, |
| 91 | + }, |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + // The test should catch the stack overflow error |
| 96 | + await expect(promise).rejects.toThrow(); |
| 97 | + |
| 98 | + // Verify that multiple calls were made (indicating recursion) |
| 99 | + expect(callCount).toBeGreaterThan(1); |
| 100 | + }, 30000); // Increased timeout for this test |
| 101 | + |
| 102 | + it('should reproduce ESM module loading recursion', async () => { |
| 103 | + // Mock SourceTextModule for ESM loading test |
| 104 | + let linkCallCount = 0; |
| 105 | + const mockModule = { |
| 106 | + link: jest.fn(async (linker) => { |
| 107 | + linkCallCount++; |
| 108 | + if (linkCallCount < 5) { |
| 109 | + // Simulate circular dependency by calling linker with same module |
| 110 | + await linker('circular-esm-module'); |
| 111 | + } |
| 112 | + }), |
| 113 | + evaluate: jest.fn(), |
| 114 | + }; |
| 115 | + |
| 116 | + mockVm.SourceTextModule.mockImplementation((code: string, options: any) => { |
| 117 | + // Trigger importModuleDynamically recursion |
| 118 | + if (options.importModuleDynamically && linkCallCount < 3) { |
| 119 | + setTimeout(() => { |
| 120 | + options.importModuleDynamically('circular-esm', {}).catch(() => {}); |
| 121 | + }, 0); |
| 122 | + } |
| 123 | + return mockModule; |
| 124 | + }); |
| 125 | + |
| 126 | + mockFetch.mockResolvedValue({ |
| 127 | + text: () => Promise.resolve('export default {};'), |
| 128 | + }); |
| 129 | + |
| 130 | + const { createScriptNode } = await import('../node'); |
| 131 | + |
| 132 | + const promise = new Promise((resolve, reject) => { |
| 133 | + createScriptNode( |
| 134 | + 'http://example.com/test.mjs', |
| 135 | + (error, scriptContext) => { |
| 136 | + if (error) { |
| 137 | + reject(error); |
| 138 | + } else { |
| 139 | + resolve(scriptContext); |
| 140 | + } |
| 141 | + }, |
| 142 | + { type: 'esm' }, |
| 143 | + { |
| 144 | + fetch: mockFetch, |
| 145 | + }, |
| 146 | + ); |
| 147 | + }); |
| 148 | + |
| 149 | + // This test demonstrates the ESM loading recursion scenario |
| 150 | + await expect(promise).rejects.toThrow(); |
| 151 | + expect(linkCallCount).toBeGreaterThan(1); |
| 152 | + }, 30000); |
| 153 | +}); |
0 commit comments