|
1 | 1 | import { afterAll, describe, expect } from 'vitest';
|
2 | 2 | import { cleanupChildProcesses, createEsmAndCjsTests } from '../../utils/runner';
|
3 | 3 |
|
4 |
| -describe('vercel xxx', () => { |
| 4 | +describe('vercel', () => { |
5 | 5 | afterAll(() => {
|
6 | 6 | cleanupChildProcesses();
|
7 | 7 | });
|
8 | 8 |
|
9 | 9 | createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => {
|
10 |
| - test('should flush events correctly on Vercel', async () => { |
| 10 | + test('should flush spans correctly on Vercel', async () => { |
11 | 11 | const runner = createRunner()
|
12 | 12 | .expect({
|
13 | 13 | transaction: {
|
@@ -47,7 +47,134 @@ describe('vercel xxx', () => {
|
47 | 47 | }
|
48 | 48 | }
|
49 | 49 |
|
50 |
| - expect(expectedLogs).toEqual([]); |
| 50 | + if (expectedLogs.length > 0) { |
| 51 | + // eslint-disable-next-line no-console |
| 52 | + console.log(actualLogs); |
| 53 | + expect(expectedLogs).toEqual([]); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + test('should flush errors correctly on Vercel', async () => { |
| 58 | + const runner = createRunner() |
| 59 | + .expect({ |
| 60 | + transaction: { |
| 61 | + transaction: 'GET /test/error', |
| 62 | + }, |
| 63 | + }) |
| 64 | + .expect({ |
| 65 | + event: { |
| 66 | + transaction: 'GET /test/error', |
| 67 | + exception: { |
| 68 | + values: [ |
| 69 | + { |
| 70 | + value: 'test error', |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) |
| 76 | + .start(); |
| 77 | + runner.makeRequest('get', '/test/error', { expectError: true }); |
| 78 | + await runner.completed(); |
| 79 | + |
| 80 | + const actualLogs = runner.getLogs(); |
| 81 | + |
| 82 | + // We want to test that the following logs are present in this order |
| 83 | + // other logs may be in between |
| 84 | + const expectedLogs = [ |
| 85 | + 'Sentry Logger [log]: @sentry/instrumentation-http Patching server.emit', |
| 86 | + 'Sentry Logger [log]: @sentry/instrumentation-http Handling incoming request', |
| 87 | + 'Sentry Logger [log]: @sentry/instrumentation-http Patching request.on', |
| 88 | + 'Sentry Logger [debug]: @opentelemetry_sentry-patched/instrumentation-http http instrumentation incomingRequest', |
| 89 | + 'Sentry Logger [log]: [Tracing] Starting sampled root span', |
| 90 | + // later... |
| 91 | + 'Sentry Logger [log]: Patching response to flush on Vercel', |
| 92 | + 'Sentry Logger [log]: Patching res.end()', |
| 93 | + // later... |
| 94 | + 'Sentry Logger [log]: Captured error event `test error`', |
| 95 | + // later... |
| 96 | + 'Sentry Logger [log]: Flushing events before Vercel Lambda freeze', |
| 97 | + 'Sentry Logger [log]: SpanExporter exported 4 spans, 0 spans are waiting for their parent spans to finish', |
| 98 | + ]; |
| 99 | + |
| 100 | + // Test that the order of logs is correct |
| 101 | + for (const log of actualLogs) { |
| 102 | + if (expectedLogs.length === 0) { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + if (log === expectedLogs[0]) { |
| 107 | + expectedLogs.shift(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (expectedLogs.length > 0) { |
| 112 | + // eslint-disable-next-line no-console |
| 113 | + console.log(actualLogs); |
| 114 | + expect(expectedLogs).toEqual([]); |
| 115 | + } |
51 | 116 | });
|
52 | 117 | });
|
| 118 | + |
| 119 | + describe('without http.server spans', () => { |
| 120 | + createEsmAndCjsTests( |
| 121 | + __dirname, |
| 122 | + 'scenario-withoutSpans.mjs', |
| 123 | + 'instrument-withoutSpans.mjs', |
| 124 | + (createRunner, test) => { |
| 125 | + test('should flush errors correctly on Vercel even without HTTP span instrumentation', async () => { |
| 126 | + const runner = createRunner() |
| 127 | + .expect({ |
| 128 | + event: { |
| 129 | + transaction: 'GET /test/error', |
| 130 | + exception: { |
| 131 | + values: [ |
| 132 | + { |
| 133 | + value: 'test error', |
| 134 | + }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + }, |
| 138 | + }) |
| 139 | + .start(); |
| 140 | + runner.makeRequest('get', '/test/error', { expectError: true }); |
| 141 | + await runner.completed(); |
| 142 | + |
| 143 | + const actualLogs = runner.getLogs(); |
| 144 | + |
| 145 | + // We want to test that the following logs are present in this order |
| 146 | + // other logs may be in between |
| 147 | + const expectedLogs = [ |
| 148 | + 'Sentry Logger [log]: @sentry/instrumentation-http Patching server.emit', |
| 149 | + 'Sentry Logger [log]: Patching response to flush on Vercel', |
| 150 | + 'Sentry Logger [log]: Patching res.end()', |
| 151 | + 'Sentry Logger [log]: @sentry/instrumentation-http Handling incoming request', |
| 152 | + 'Sentry Logger [log]: @sentry/instrumentation-http Patching request.on', |
| 153 | + // later... |
| 154 | + 'Sentry Logger [log]: Captured error event `test error`', |
| 155 | + // later... |
| 156 | + 'Sentry Logger [log]: Flushing events before Vercel Lambda freeze', |
| 157 | + 'Sentry Logger [log]: SpanExporter exported 0 spans, 0 spans are waiting for their parent spans to finish', |
| 158 | + ]; |
| 159 | + |
| 160 | + // Test that the order of logs is correct |
| 161 | + for (const log of actualLogs) { |
| 162 | + if (expectedLogs.length === 0) { |
| 163 | + break; |
| 164 | + } |
| 165 | + |
| 166 | + if (log === expectedLogs[0]) { |
| 167 | + expectedLogs.shift(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (expectedLogs.length > 0) { |
| 172 | + // eslint-disable-next-line no-console |
| 173 | + console.log(actualLogs); |
| 174 | + expect(expectedLogs).toEqual([]); |
| 175 | + } |
| 176 | + }); |
| 177 | + }, |
| 178 | + ); |
| 179 | + }); |
53 | 180 | });
|
0 commit comments