diff options
Diffstat (limited to 'packages/integrations/web-vitals/test/basics.test.js')
-rw-r--r-- | packages/integrations/web-vitals/test/basics.test.js | 59 |
1 files changed, 47 insertions, 12 deletions
diff --git a/packages/integrations/web-vitals/test/basics.test.js b/packages/integrations/web-vitals/test/basics.test.js index 937619b48..43814b56d 100644 --- a/packages/integrations/web-vitals/test/basics.test.js +++ b/packages/integrations/web-vitals/test/basics.test.js @@ -5,6 +5,12 @@ import { after, before, beforeEach, describe, it } from 'node:test'; import { parseHTML } from 'linkedom'; import { loadFixture } from './test-utils.js'; +function startOfHourISOString() { + const date = new Date(); + date.setMinutes(0, 0, 0); + return date.toISOString(); +} + /** * @template {Record<K, (...args: any[]) => void>} T * @template {keyof T} K @@ -94,25 +100,54 @@ describe('Web Vitals integration basics', () => { assert.equal(call.name, 'ZodError'); }); - it('inserts data via the injected endpoint', async () => { - const res = await fixture.fetch('/_web-vitals', { - method: 'POST', - body: JSON.stringify([ + describe('inserting data via the injected endpoint', () => { + /** @type {Response} */ + let res; + before(async () => { + res = await fixture.fetch('/_web-vitals', { + method: 'POST', + body: JSON.stringify([ + { + pathname: '/', + route: '/', + name: 'CLS', + id: 'v4-1711484350895-3748043125387', + value: 0, + rating: 'good', + }, + ]), + }); + }); + + it('inserting data does not error', () => { + assert.equal(res.status, 200); + assert.equal( + consoleErrorMock.calls.length, + 0, + 'Endpoint logged errors:\n' + consoleErrorMock.calls[0]?.join(' ') + ); + }) + + it('inserted data can be retrieved from the database', async () => { + const dbRows = await fixture.fetch('/rows.json', {}).then((r) => r.json()); + assert.deepEqual(dbRows, [ { pathname: '/', route: '/', name: 'CLS', - id: 'v3-1711484350895-3748043125387', + id: 'v4-17114843-3748043125387', value: 0, rating: 'good', + timestamp: startOfHourISOString(), }, - ]), + ]); }); - assert.equal(res.status, 200); - assert.equal( - consoleErrorMock.calls.length, - 0, - 'Endpoint logged errors:\n' + consoleErrorMock.calls[0]?.join(' ') - ); + }); + + it('inserted data uses a truncated timestamp in the ID', async () => { + // The IDs generated by the `web-vitals` package include a high resolution timestamp as the second portion, + // e.g. 'v4-1711484350895-3748043125387'. We reduce this data to an hourly resolution to lessen privacy concerns. + const dbRows = await fixture.fetch('/rows.json', {}).then((r) => r.json()); + assert.deepEqual(dbRows[0].id, 'v4-17114843-3748043125387'); }); }); |