|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 2 | +import * as flushModule from '../../../src/exports'; |
| 3 | +import { flushIfServerless } from '../../../src/utils/flushIfServerless'; |
| 4 | +import * as vercelWaitUntilModule from '../../../src/utils/vercelWaitUntil'; |
| 5 | +import { GLOBAL_OBJ } from '../../../src/utils/worldwide'; |
| 6 | + |
| 7 | +describe('flushIfServerless', () => { |
| 8 | + let originalProcess: typeof process; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + vi.resetAllMocks(); |
| 12 | + originalProcess = global.process; |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + vi.restoreAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + test('should bind context (preserve `this`) when calling waitUntil from the Cloudflare execution context', async () => { |
| 20 | + const flushMock = vi.spyOn(flushModule, 'flush').mockResolvedValue(true); |
| 21 | + |
| 22 | + // Mock Cloudflare context with `waitUntil` (which should be called if `this` is bound correctly) |
| 23 | + const mockCloudflareCtx = { |
| 24 | + contextData: 'test-data', |
| 25 | + waitUntil: function (promise: Promise<unknown>) { |
| 26 | + // This will fail if 'this' is not bound correctly |
| 27 | + expect(this.contextData).toBe('test-data'); |
| 28 | + return promise; |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + const waitUntilSpy = vi.spyOn(mockCloudflareCtx, 'waitUntil'); |
| 33 | + |
| 34 | + await flushIfServerless({ cloudflareCtx: mockCloudflareCtx }); |
| 35 | + |
| 36 | + expect(waitUntilSpy).toHaveBeenCalledTimes(1); |
| 37 | + expect(flushMock).toHaveBeenCalledWith(2000); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should use cloudflare waitUntil when valid cloudflare context is provided', async () => { |
| 41 | + const flushMock = vi.spyOn(flushModule, 'flush').mockResolvedValue(true); |
| 42 | + const mockCloudflareCtx = { |
| 43 | + waitUntil: vi.fn(), |
| 44 | + }; |
| 45 | + |
| 46 | + await flushIfServerless({ cloudflareCtx: mockCloudflareCtx, timeout: 5000 }); |
| 47 | + |
| 48 | + expect(mockCloudflareCtx.waitUntil).toHaveBeenCalledTimes(1); |
| 49 | + expect(flushMock).toHaveBeenCalledWith(5000); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should use cloudflare waitUntil when Cloudflare `waitUntil` is provided', async () => { |
| 53 | + const flushMock = vi.spyOn(flushModule, 'flush').mockResolvedValue(true); |
| 54 | + const mockCloudflareCtx = { |
| 55 | + waitUntil: vi.fn(), |
| 56 | + }; |
| 57 | + |
| 58 | + await flushIfServerless({ cloudflareWaitUntil: mockCloudflareCtx.waitUntil, timeout: 5000 }); |
| 59 | + |
| 60 | + expect(mockCloudflareCtx.waitUntil).toHaveBeenCalledTimes(1); |
| 61 | + expect(flushMock).toHaveBeenCalledWith(5000); |
| 62 | + }); |
| 63 | + |
| 64 | + test('should ignore cloudflare context when waitUntil is not a function (and use Vercel waitUntil instead)', async () => { |
| 65 | + const flushMock = vi.spyOn(flushModule, 'flush').mockResolvedValue(true); |
| 66 | + const vercelWaitUntilSpy = vi.spyOn(vercelWaitUntilModule, 'vercelWaitUntil').mockImplementation(() => {}); |
| 67 | + |
| 68 | + // Mock Vercel environment |
| 69 | + // @ts-expect-error This is not typed |
| 70 | + GLOBAL_OBJ[Symbol.for('@vercel/request-context')] = { get: () => ({ waitUntil: vi.fn() }) }; |
| 71 | + |
| 72 | + const mockCloudflareCtx = { |
| 73 | + waitUntil: 'not-a-function', // Invalid waitUntil |
| 74 | + }; |
| 75 | + |
| 76 | + // @ts-expect-error Using the wrong type here on purpose |
| 77 | + await flushIfServerless({ cloudflareCtx: mockCloudflareCtx }); |
| 78 | + |
| 79 | + expect(vercelWaitUntilSpy).toHaveBeenCalledTimes(1); |
| 80 | + expect(flushMock).toHaveBeenCalledWith(2000); |
| 81 | + }); |
| 82 | + |
| 83 | + test('should handle multiple serverless environment variables simultaneously', async () => { |
| 84 | + const flushMock = vi.spyOn(flushModule, 'flush').mockResolvedValue(true); |
| 85 | + |
| 86 | + global.process = { |
| 87 | + ...originalProcess, |
| 88 | + env: { |
| 89 | + ...originalProcess.env, |
| 90 | + LAMBDA_TASK_ROOT: '/var/task', |
| 91 | + VERCEL: '1', |
| 92 | + NETLIFY: 'true', |
| 93 | + CF_PAGES: '1', |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + await flushIfServerless({ timeout: 4000 }); |
| 98 | + |
| 99 | + expect(flushMock).toHaveBeenCalledWith(4000); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should use default timeout when not specified', async () => { |
| 103 | + const flushMock = vi.spyOn(flushModule, 'flush').mockResolvedValue(true); |
| 104 | + const mockCloudflareCtx = { |
| 105 | + waitUntil: vi.fn(), |
| 106 | + }; |
| 107 | + |
| 108 | + await flushIfServerless({ cloudflareCtx: mockCloudflareCtx }); |
| 109 | + |
| 110 | + expect(flushMock).toHaveBeenCalledWith(2000); |
| 111 | + }); |
| 112 | + |
| 113 | + test('should handle zero timeout value', async () => { |
| 114 | + const flushMock = vi.spyOn(flushModule, 'flush').mockResolvedValue(true); |
| 115 | + |
| 116 | + global.process = { |
| 117 | + ...originalProcess, |
| 118 | + env: { |
| 119 | + ...originalProcess.env, |
| 120 | + LAMBDA_TASK_ROOT: '/var/task', |
| 121 | + }, |
| 122 | + }; |
| 123 | + |
| 124 | + await flushIfServerless({ timeout: 0 }); |
| 125 | + |
| 126 | + expect(flushMock).toHaveBeenCalledWith(0); |
| 127 | + }); |
| 128 | +}); |
0 commit comments