|
| 1 | +import type { Dispatcher } from 'undici'; |
| 2 | + |
| 3 | +const WARNING_MESSAGE = |
| 4 | + 'You have supplied a `Dispatcher` to `createHttpTransport()`. It has been ignored because ' + |
| 5 | + 'Undici dispatchers only work in Node environments. To eliminate this warning, omit the ' + |
| 6 | + '`dispatcher_NODE_ONLY` property from your config when running in a non-Node environment.'; |
| 7 | + |
| 8 | +describe('createHttpTransport()', () => { |
| 9 | + let createHttpTransport: typeof import('../http-transport').createHttpTransport; |
| 10 | + beforeEach(async () => { |
| 11 | + jest.spyOn(console, 'warn').mockImplementation(); |
| 12 | + await jest.isolateModulesAsync(async () => { |
| 13 | + createHttpTransport = |
| 14 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 15 | + // @ts-ignore |
| 16 | + (await import('../http-transport')).createHttpTransport; |
| 17 | + }); |
| 18 | + }); |
| 19 | + describe('in development mode', () => { |
| 20 | + beforeEach(() => { |
| 21 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 22 | + (globalThis as any).__DEV__ = true; |
| 23 | + }); |
| 24 | + it('warns when configured with a dispatcher', () => { |
| 25 | + createHttpTransport({ dispatcher_NODE_ONLY: {} as Dispatcher, url: 'fake://url' }); |
| 26 | + expect(console.warn).toHaveBeenCalledWith(WARNING_MESSAGE); |
| 27 | + }); |
| 28 | + it('warns when configured with an undefined dispatcher', () => { |
| 29 | + createHttpTransport({ dispatcher_NODE_ONLY: undefined, url: 'fake://url' }); |
| 30 | + expect(console.warn).toHaveBeenCalledWith(WARNING_MESSAGE); |
| 31 | + }); |
| 32 | + it('only warns once no matter how many times it is configured with a dispatcher', () => { |
| 33 | + createHttpTransport({ dispatcher_NODE_ONLY: undefined, url: 'fake://url' }); |
| 34 | + createHttpTransport({ dispatcher_NODE_ONLY: {} as Dispatcher, url: 'fake://url' }); |
| 35 | + createHttpTransport({ dispatcher_NODE_ONLY: null as unknown as Dispatcher, url: 'fake://url' }); |
| 36 | + expect(console.warn).toHaveBeenCalledTimes(1); |
| 37 | + }); |
| 38 | + }); |
| 39 | + describe('in production mode', () => { |
| 40 | + beforeEach(() => { |
| 41 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 42 | + (globalThis as any).__DEV__ = false; |
| 43 | + }); |
| 44 | + it('does not warn when configured with a dispatcher', () => { |
| 45 | + createHttpTransport({ dispatcher_NODE_ONLY: {} as Dispatcher, url: 'fake://url' }); |
| 46 | + expect(console.warn).not.toHaveBeenCalledWith(WARNING_MESSAGE); |
| 47 | + }); |
| 48 | + it('does not warn when configured with an undefined dispatcher', () => { |
| 49 | + createHttpTransport({ dispatcher_NODE_ONLY: undefined, url: 'fake://url' }); |
| 50 | + expect(console.warn).not.toHaveBeenCalledWith(WARNING_MESSAGE); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments