-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
25 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages/next/src/telemetry/post-telemetry-payload.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { postNextTelemetryPayload } from './post-telemetry-payload' | ||
|
||
describe('postNextTelemetryPayload', () => { | ||
let originalFetch: typeof fetch | ||
|
||
beforeEach(() => { | ||
originalFetch = global.fetch | ||
}) | ||
|
||
afterEach(() => { | ||
global.fetch = originalFetch | ||
}) | ||
|
||
it('sends telemetry payload successfully', async () => { | ||
const mockFetch = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
}) | ||
global.fetch = mockFetch | ||
|
||
const payload = { | ||
meta: { version: '1.0' }, | ||
context: { | ||
anonymousId: 'test-id', | ||
projectId: 'test-project', | ||
sessionId: 'test-session', | ||
}, | ||
events: [ | ||
{ | ||
eventName: 'test-event', | ||
fields: { foo: 'bar' }, | ||
}, | ||
], | ||
} | ||
|
||
await postNextTelemetryPayload(payload) | ||
|
||
expect(mockFetch).toHaveBeenCalledWith( | ||
'https://telemetry.nextjs.org/api/v1/record', | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
headers: { 'content-type': 'application/json' }, | ||
signal: expect.any(AbortSignal), | ||
} | ||
) | ||
}) | ||
|
||
it('retries on failure', async () => { | ||
const mockFetch = jest | ||
.fn() | ||
.mockRejectedValueOnce(new Error('Network error')) | ||
.mockResolvedValueOnce({ ok: true }) | ||
global.fetch = mockFetch | ||
|
||
const payload = { | ||
meta: {}, | ||
context: { | ||
anonymousId: 'test-id', | ||
projectId: 'test-project', | ||
sessionId: 'test-session', | ||
}, | ||
events: [], | ||
} | ||
|
||
await postNextTelemetryPayload(payload) | ||
|
||
expect(mockFetch).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('swallows errors after retries exhausted', async () => { | ||
const mockFetch = jest.fn().mockRejectedValue(new Error('Network error')) | ||
global.fetch = mockFetch | ||
|
||
const payload = { | ||
meta: {}, | ||
context: { | ||
anonymousId: 'test-id', | ||
projectId: 'test-project', | ||
sessionId: 'test-session', | ||
}, | ||
events: [], | ||
} | ||
|
||
// Should not throw | ||
await postNextTelemetryPayload(payload) | ||
|
||
expect(mockFetch).toHaveBeenCalledTimes(2) // Initial try + 1 retry | ||
}) | ||
}) |
21 changes: 18 additions & 3 deletions
21
packages/next/src/telemetry/post-payload.ts → ...t/src/telemetry/post-telemetry-payload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters