| 30 | }; |
| 31 | |
| 32 | function makeLoader(overrides: Partial<Stubs> = {}) { |
| 33 | const logFormattedReferenceError = jest.fn(); |
| 34 | const stubs: Stubs = { |
| 35 | coreModule: { |
| 36 | require: jest.fn(), |
| 37 | } as unknown as jest.Mocked<CoreModuleProvider>, |
| 38 | environment: { |
| 39 | global: {JSON} as unknown as JestEnvironment['global'], |
| 40 | } as JestEnvironment, |
| 41 | executor: { |
| 42 | exec: jest.fn(() => 'loaded' as const), |
| 43 | getCurrentlyExecutingManualMock: jest.fn(() => null), |
| 44 | mainModule: null, |
| 45 | resetMainModule: jest.fn(), |
| 46 | } as unknown as jest.Mocked<ModuleExecutor>, |
| 47 | logFormattedReferenceError, |
| 48 | mockState: { |
| 49 | getCjsModuleId: jest.fn(() => 'id'), |
| 50 | isExplicitlyUnmocked: jest.fn(() => false), |
| 51 | } as unknown as jest.Mocked<MockState>, |
| 52 | registries: { |
| 53 | getActiveCjsRegistry: jest.fn(() => new Map()), |
| 54 | getActiveEsmRegistry: jest.fn(() => new Map()), |
| 55 | } as unknown as jest.Mocked<ModuleRegistries>, |
| 56 | requireEsm: jest.fn() as any, |
| 57 | resolution: { |
| 58 | getCjsMockModule: jest.fn(() => null), |
| 59 | getModule: jest.fn(() => null), |
| 60 | isCoreModule: jest.fn(() => false), |
| 61 | resolveCjs: jest.fn(() => '/m.js'), |
| 62 | shouldLoadAsEsm: jest.fn(() => false), |
| 63 | } as unknown as jest.Mocked<Resolution>, |
| 64 | testState: new TestState(logFormattedReferenceError), |
| 65 | transformCache: { |
| 66 | transformJson: jest.fn((_path, _opts) => '{"answer":42}'), |
| 67 | } as unknown as jest.Mocked<TransformCache>, |
| 68 | ...overrides, |
| 69 | }; |
| 70 | const loader = new CjsLoader({ |
| 71 | coreModule: stubs.coreModule, |
| 72 | environment: stubs.environment, |
| 73 | executor: stubs.executor, |
| 74 | logFormattedReferenceError: stubs.logFormattedReferenceError, |
| 75 | mockState: stubs.mockState, |
| 76 | registries: stubs.registries, |
| 77 | requireEsm: stubs.requireEsm, |
| 78 | resolution: stubs.resolution, |
| 79 | testState: stubs.testState, |
| 80 | transformCache: stubs.transformCache, |
| 81 | }); |
| 82 | return {loader, stubs}; |
| 83 | } |
| 84 | |
| 85 | describe('CjsLoader.requireModule', () => { |
| 86 | test('routes core modules to coreModule.require', () => { |