| 1 | # Test Template |
| 2 | |
| 3 | Complete test template for `packages/evlog/test/adapters/{name}.test.ts`. |
| 4 | |
| 5 | Replace `{Name}`, `{name}` with the actual service name. |
| 6 | |
| 7 | ```typescript |
| 8 | import { afterEach, beforeEach |
| 9 | import type { WideEvent |
| 10 | import { sendBatchTo{Name |
| 11 | |
| 12 | describe('{name} adapter', () |
| 13 | let fetchSpy: |
| 14 | |
| 15 | // --- Setup: mock globalThis.fetch to return 200 --- |
| 16 | beforeEach(() => |
| 17 | fetchSpy = vi. |
| 18 | new Response(null, |
| 19 | ) |
| 20 | }) |
| 21 | |
| 22 | afterEach(() => |
| 23 | vi.restoreAllMocks() |
| 24 | }) |
| 25 | |
| 26 | // --- Test event factory --- |
| 27 | const createTestEvent = |
| 28 | timestamp: '2024-01-01T12:00:00.000Z', |
| 29 | level: 'info', |
| 30 | service: 'test-service', |
| 31 | environment: 'test', |
| 32 | ...overrides, |
| 33 | }) |
| 34 | |
| 35 | // --- 1. URL Construction --- |
| 36 | describe('sendTo{Name}', |
| 37 | it('sends event to correct URL', |
| 38 | const event = |
| 39 | |
| 40 | await sendTo{Name |
| 41 | apiKey: 'test-key', |
| 42 | }) |
| 43 | |
| 44 | expect(fetchSpy).toHaveBeenCalledTimes |
| 45 | const [url] |
| 46 | // Verify the default endpoint URL |
| 47 | expect(url).toBe |
| 48 | }) |
| 49 | |
| 50 | it('uses custom endpoint when provided', |
| 51 | const event = |
| 52 | |
| 53 | await sendTo{Name |
| 54 | apiKey: 'test-key', |
| 55 | endpoint: 'https://custom.{name}.com', |
| 56 | }) |
| 57 | |
| 58 | const [url] |
| 59 | expect(url).toBe |
| 60 | }) |
| 61 | |
| 62 | // --- 2. Headers --- |
| 63 | it('sets correct Authorization header', |
| 64 | const event = |
| 65 | |
| 66 | await sendTo{Name |
| 67 | apiKey: 'my-secret-key', |
| 68 | }) |
| 69 | |
| 70 | const [, options |
| 71 | expect(options.headers |
| 72 | 'Authorization': 'Bearer my-secret-key', |
| 73 | })) |
| 74 | }) |
| 75 | |
| 76 | it('sets Content-Type to application/json', |
| 77 | const event = |
| 78 | |
| 79 | await sendTo{Name |
| 80 | apiKey: 'test-key', |
| 81 | }) |
| 82 | |
| 83 | const [, options |
| 84 | expect(options.headers |
| 85 | 'Content-Type': 'application/json', |
| 86 | })) |
| 87 | }) |
| 88 | |
| 89 | // Add service-specific header tests here |
| 90 | // Example: orgId, project header, region header, etc. |
| 91 | |
| 92 | // --- 3. Request Body --- |
| 93 | it('sends event in correct format', |
| 94 | const event = |
| 95 | |
| 96 | await sendTo{Name |
| 97 | apiKey: 'test-key', |
| 98 | }) |
| 99 | |
| 100 | const [, options |
| 101 | const body = |
| 102 | // Verify the body matches the expected format |
| 103 | // Adapt this to match the service's expected payload structure |
| 104 | expect(body).toBeInstanceOf |
| 105 | expect(body).toHaveLength |
| 106 | }) |
| 107 | |
| 108 | // --- 4. Error Handling --- |
| 109 | it('throws error on non-OK response', |
| 110 | fetchSpy.mockResolvedValueOnce( |
| 111 | new Response('Bad Request |
| 112 | ) |
| 113 | |
| 114 | const event = |
| 115 | |
| 116 | await expect(sendTo |
| 117 | apiKey: 'test-key', |
| 118 | })).rejects.toThrow |
| 119 | }) |
| 120 | }) |
| 121 | |
| 122 | // --- 5. Batch Operations --- |
| 123 | describe('sendBatchTo{Name}', |
| 124 | it('sends multiple events in a single request', |
| 125 | const events = |
| 126 | createTestEvent({ requestId: |
| 127 | createTestEvent({ requestId: |
| 128 | createTestEvent({ requestId: |
| 129 | ] |
| 130 | |
| 131 | await sendBatchTo{Name |
| 132 | apiKey: 'test-key', |
| 133 | }) |
| 134 | |
| 135 | expect(fetchSpy).toHaveBeenCalledTimes |
| 136 | const [, options |
| 137 | const body = |
| 138 | expect(body).toHaveLength |
| 139 | }) |
| 140 | |
| 141 | it('skips fetch when events array is empty', |
| 142 | await sendBatchTo{Name |
| 143 | apiKey: 'test-key', |
| 144 | }) |
| 145 | |
| 146 | expect(fetchSpy).not |
| 147 | }) |
| 148 | }) |
| 149 | |
| 150 | // --- 6. Timeout Handling --- |
| 151 | describe('timeout handling', |
| 152 | it('uses default timeout of 5000ms', |
| 153 | const event = |
| 154 | const setTimeoutSpy = |
| 155 | |
| 156 | await sendTo{Name |
| 157 | apiKey: 'test-key', |
| 158 | }) |
| 159 | |
| 160 | expect(setTimeoutSpy).toHaveBeenCalledWith |
| 161 | }) |
| 162 | |
| 163 | it('uses custom timeout when provided', |
| 164 | const event = |
| 165 | const setTimeoutSpy = |
| 166 | |
| 167 | await sendTo{Name |
| 168 | apiKey: 'test-key', |
| 169 | timeout: 10000, |
| 170 | }) |
| 171 | |
| 172 | expect(setTimeoutSpy).toHaveBeenCalledWith |
| 173 | }) |
| 174 | }) |
| 175 | }) |
| 176 | ``` |
| 177 | |
| 178 | ## Customization Notes |
| 179 | |
| 180 | - **URL assertions**: Update the expected URLs to match the actual service API. |
| 181 | - **Auth headers**: If the service uses a custom auth header (e.g., |
| 182 | - **Body format**: Adapt body assertions to match the service's expected payload. Some services wrap events in an object (PostHog: |
| 183 | - **Empty batch**: The template asserts |
| 184 | - **Event transformation**: If you export a |
| 185 | - **Service-specific tests**: Add tests for any service-specific features (e.g., Axiom's |