|
| 1 | +import { _resetBunWarning, isBunRuntime, warnIfBunRuntime } from '@crawlee/utils'; |
| 2 | + |
| 3 | +afterEach(() => { |
| 4 | + _resetBunWarning(); |
| 5 | + delete (globalThis as any).Bun; |
| 6 | +}); |
| 7 | + |
| 8 | +describe('isBunRuntime', () => { |
| 9 | + test('returns false when Bun global is not present', () => { |
| 10 | + delete (globalThis as any).Bun; |
| 11 | + expect(isBunRuntime()).toBe(false); |
| 12 | + }); |
| 13 | + |
| 14 | + test('returns true when Bun global is present', () => { |
| 15 | + (globalThis as any).Bun = { version: '1.0.0' }; |
| 16 | + expect(isBunRuntime()).toBe(true); |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +describe('warnIfBunRuntime', () => { |
| 21 | + test('does nothing when not running under Bun', () => { |
| 22 | + delete (globalThis as any).Bun; |
| 23 | + const logger = { warning: vitest.fn() }; |
| 24 | + warnIfBunRuntime(logger); |
| 25 | + expect(logger.warning).not.toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('calls logger.warning when running under Bun', () => { |
| 29 | + (globalThis as any).Bun = { version: '1.0.0' }; |
| 30 | + const logger = { warning: vitest.fn() }; |
| 31 | + warnIfBunRuntime(logger); |
| 32 | + expect(logger.warning).toHaveBeenCalledTimes(1); |
| 33 | + expect(logger.warning).toHaveBeenCalledWith(expect.stringContaining('ImpitHttpClient')); |
| 34 | + }); |
| 35 | + |
| 36 | + test('only warns once even if called multiple times', () => { |
| 37 | + (globalThis as any).Bun = { version: '1.0.0' }; |
| 38 | + const logger = { warning: vitest.fn() }; |
| 39 | + warnIfBunRuntime(logger); |
| 40 | + warnIfBunRuntime(logger); |
| 41 | + warnIfBunRuntime(logger); |
| 42 | + expect(logger.warning).toHaveBeenCalledTimes(1); |
| 43 | + }); |
| 44 | + |
| 45 | + test('falls back to console.warn when no logger is provided', () => { |
| 46 | + (globalThis as any).Bun = { version: '1.0.0' }; |
| 47 | + const spy = vitest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 48 | + warnIfBunRuntime(); |
| 49 | + expect(spy).toHaveBeenCalledTimes(1); |
| 50 | + expect(spy).toHaveBeenCalledWith(expect.stringContaining('ImpitHttpClient')); |
| 51 | + }); |
| 52 | +}); |
0 commit comments