|
| 1 | +/** |
| 2 | + * Tests for CJS fetchWrapper implementation |
| 3 | + */ |
| 4 | + |
| 5 | +// Import types are only used for dynamic imports in tests, so we don't import them here |
| 6 | +// The functions are imported dynamically within each test to ensure proper module isolation |
| 7 | + |
| 8 | +// Mock the dynamic import to avoid actually importing node-fetch |
| 9 | +const mockNodeFetch = { |
| 10 | + default: jest.fn().mockName('mockFetch'), |
| 11 | + Request: jest.fn().mockName('mockRequest'), |
| 12 | + Response: jest.fn().mockName('mockResponse'), |
| 13 | +}; |
| 14 | + |
| 15 | +// Mock the Function constructor used for dynamic imports |
| 16 | +const mockDynamicImport = jest.fn().mockResolvedValue(mockNodeFetch); |
| 17 | +global.Function = jest.fn().mockImplementation(() => mockDynamicImport); |
| 18 | + |
| 19 | +// Mock global objects for test environment detection |
| 20 | +const originalGlobal = global; |
| 21 | + |
| 22 | +describe('fetchWrapper-cjs', () => { |
| 23 | + beforeEach(() => { |
| 24 | + jest.clearAllMocks(); |
| 25 | + // Reset the module cache by clearing the nodeFetchModule cache |
| 26 | + // This is done by reimporting the module |
| 27 | + jest.resetModules(); |
| 28 | + // Setup mocked Function constructor |
| 29 | + global.Function = jest.fn().mockImplementation(() => mockDynamicImport); |
| 30 | + // Reset mock implementation |
| 31 | + mockDynamicImport.mockResolvedValue(mockNodeFetch); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('getFetch', () => { |
| 35 | + it('should return global.fetch when in test environment', async () => { |
| 36 | + const mockGlobalFetch = jest.fn().mockName('globalFetch'); |
| 37 | + (global as any).fetch = mockGlobalFetch; |
| 38 | + |
| 39 | + const { getFetch } = await import('../../src/utils/fetchWrapper-cjs.js'); |
| 40 | + const fetch = await getFetch(); |
| 41 | + |
| 42 | + expect(fetch).toBe(mockGlobalFetch); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should use dynamic import when global.fetch is not available', async () => { |
| 46 | + // Remove global.fetch |
| 47 | + delete (global as any).fetch; |
| 48 | + |
| 49 | + const { getFetch } = await import('../../src/utils/fetchWrapper-cjs.js'); |
| 50 | + const fetch = await getFetch(); |
| 51 | + |
| 52 | + expect(fetch).toBe(mockNodeFetch.default); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return a function', async () => { |
| 56 | + delete (global as any).fetch; |
| 57 | + |
| 58 | + const { getFetch } = await import('../../src/utils/fetchWrapper-cjs.js'); |
| 59 | + const fetch = await getFetch(); |
| 60 | + |
| 61 | + expect(typeof fetch).toBe('function'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle Function constructor usage', async () => { |
| 65 | + delete (global as any).fetch; |
| 66 | + |
| 67 | + const { getFetch } = await import('../../src/utils/fetchWrapper-cjs.js'); |
| 68 | + await getFetch(); |
| 69 | + |
| 70 | + // Verify Function constructor was called |
| 71 | + expect(global.Function).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('getRequest', () => { |
| 76 | + it('should return global.Request when in test environment', async () => { |
| 77 | + const mockGlobalRequest = jest.fn().mockName('globalRequest'); |
| 78 | + (global as any).Request = mockGlobalRequest; |
| 79 | + |
| 80 | + const { getRequest } = await import( |
| 81 | + '../../src/utils/fetchWrapper-cjs.js' |
| 82 | + ); |
| 83 | + const Request = await getRequest(); |
| 84 | + |
| 85 | + expect(Request).toBe(mockGlobalRequest); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should use dynamic import when global.Request is not available', async () => { |
| 89 | + delete (global as any).Request; |
| 90 | + |
| 91 | + const { getRequest } = await import( |
| 92 | + '../../src/utils/fetchWrapper-cjs.js' |
| 93 | + ); |
| 94 | + const Request = await getRequest(); |
| 95 | + |
| 96 | + expect(Request).toBe(mockNodeFetch.Request); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should return a function', async () => { |
| 100 | + delete (global as any).Request; |
| 101 | + |
| 102 | + const { getRequest } = await import( |
| 103 | + '../../src/utils/fetchWrapper-cjs.js' |
| 104 | + ); |
| 105 | + const Request = await getRequest(); |
| 106 | + |
| 107 | + expect(typeof Request).toBe('function'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('getResponse', () => { |
| 112 | + it('should return global.Response when in test environment', async () => { |
| 113 | + const mockGlobalResponse = jest.fn().mockName('globalResponse'); |
| 114 | + (global as any).Response = mockGlobalResponse; |
| 115 | + |
| 116 | + const { getResponse } = await import( |
| 117 | + '../../src/utils/fetchWrapper-cjs.js' |
| 118 | + ); |
| 119 | + const Response = await getResponse(); |
| 120 | + |
| 121 | + expect(Response).toBe(mockGlobalResponse); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should use dynamic import when global.Response is not available', async () => { |
| 125 | + delete (global as any).Response; |
| 126 | + |
| 127 | + const { getResponse } = await import( |
| 128 | + '../../src/utils/fetchWrapper-cjs.js' |
| 129 | + ); |
| 130 | + const Response = await getResponse(); |
| 131 | + |
| 132 | + expect(Response).toBe(mockNodeFetch.Response); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should return a function', async () => { |
| 136 | + delete (global as any).Response; |
| 137 | + |
| 138 | + const { getResponse } = await import( |
| 139 | + '../../src/utils/fetchWrapper-cjs.js' |
| 140 | + ); |
| 141 | + const Response = await getResponse(); |
| 142 | + |
| 143 | + expect(typeof Response).toBe('function'); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('mixed environment scenarios', () => { |
| 148 | + it('should prefer global objects when available, fall back to dynamic import for missing ones', async () => { |
| 149 | + const mockGlobalFetch = jest.fn().mockName('globalFetch'); |
| 150 | + (global as any).fetch = mockGlobalFetch; |
| 151 | + delete (global as any).Request; |
| 152 | + delete (global as any).Response; |
| 153 | + |
| 154 | + const { getFetch, getRequest, getResponse } = await import( |
| 155 | + '../../src/utils/fetchWrapper-cjs.js' |
| 156 | + ); |
| 157 | + |
| 158 | + const fetch = await getFetch(); |
| 159 | + const Request = await getRequest(); |
| 160 | + const Response = await getResponse(); |
| 161 | + |
| 162 | + // fetch should use global |
| 163 | + expect(fetch).toBe(mockGlobalFetch); |
| 164 | + |
| 165 | + // Request and Response should use dynamic import |
| 166 | + expect(Request).toBe(mockNodeFetch.Request); |
| 167 | + expect(Response).toBe(mockNodeFetch.Response); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should handle undefined global object gracefully', async () => { |
| 171 | + // Simulate environment where global might be undefined |
| 172 | + const originalGlobal = global; |
| 173 | + (global as any) = undefined; |
| 174 | + |
| 175 | + const { getFetch } = await import('../../src/utils/fetchWrapper-cjs.js'); |
| 176 | + const fetch = await getFetch(); |
| 177 | + |
| 178 | + expect(fetch).toBe(mockNodeFetch.default); |
| 179 | + |
| 180 | + // Restore global |
| 181 | + (global as any) = originalGlobal; |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe('Type exports', () => { |
| 186 | + it('should export types as any for CJS compatibility', () => { |
| 187 | + // This test ensures that the types are properly exported as 'any' |
| 188 | + // The actual type checking happens at compile time |
| 189 | + expect(true).toBe(true); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + afterEach(() => { |
| 194 | + // Clean up global modifications |
| 195 | + delete (global as any).fetch; |
| 196 | + delete (global as any).Request; |
| 197 | + delete (global as any).Response; |
| 198 | + // Restore original Function constructor if needed |
| 199 | + if (originalGlobal.Function) { |
| 200 | + global.Function = originalGlobal.Function; |
| 201 | + } |
| 202 | + }); |
| 203 | +}); |
0 commit comments