From 420aaf8fb8ce0123b9d139bfce924823f2cb6866 Mon Sep 17 00:00:00 2001 From: Sigrid Huemer <32902192+s1gr1d@users.noreply.github.com> Date: Mon, 5 Aug 2024 17:56:52 +0200 Subject: [PATCH] test(nuxt): Unit tests for event filter (#13229) Adding more convenient unit test. There is already an E2E test for this: https://github.com/getsentry/sentry-javascript/blob/b6cf7b0cabc27a84e962f0a1e408465cc87fd961/dev-packages/e2e-tests/test-applications/nuxt-3/tests/performance.server.test.ts#L24 Also moved two files in the `test` folder to resemble the `src` folder --- .../plugins/server.test.ts} | 0 .../test/{client => }/runtime/utils.test.ts | 2 +- packages/nuxt/test/server/sdk.test.ts | 43 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) rename packages/nuxt/test/{server/runtime/plugin.test.ts => runtime/plugins/server.test.ts} (100%) rename packages/nuxt/test/{client => }/runtime/utils.test.ts (97%) diff --git a/packages/nuxt/test/server/runtime/plugin.test.ts b/packages/nuxt/test/runtime/plugins/server.test.ts similarity index 100% rename from packages/nuxt/test/server/runtime/plugin.test.ts rename to packages/nuxt/test/runtime/plugins/server.test.ts diff --git a/packages/nuxt/test/client/runtime/utils.test.ts b/packages/nuxt/test/runtime/utils.test.ts similarity index 97% rename from packages/nuxt/test/client/runtime/utils.test.ts rename to packages/nuxt/test/runtime/utils.test.ts index b0b039d52e54..08c66193caa3 100644 --- a/packages/nuxt/test/client/runtime/utils.test.ts +++ b/packages/nuxt/test/runtime/utils.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { extractErrorContext } from '../../../src/runtime/utils'; +import { extractErrorContext } from '../../src/runtime/utils'; describe('extractErrorContext', () => { it('returns empty object for undefined or empty context', () => { diff --git a/packages/nuxt/test/server/sdk.test.ts b/packages/nuxt/test/server/sdk.test.ts index 8d84dc8b15c8..20ec11c33512 100644 --- a/packages/nuxt/test/server/sdk.test.ts +++ b/packages/nuxt/test/server/sdk.test.ts @@ -1,4 +1,5 @@ import * as SentryNode from '@sentry/node'; +import type { NodeClient } from '@sentry/node'; import { SDK_VERSION } from '@sentry/node'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { init } from '../../src/server'; @@ -38,5 +39,47 @@ describe('Nuxt Server SDK', () => { it('returns client from init', () => { expect(init({})).not.toBeUndefined(); }); + + it('filters out low quality transactions', async () => { + const beforeSendEvent = vi.fn(event => event); + const client = init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + }) as NodeClient; + client.on('beforeSendEvent', beforeSendEvent); + + client.captureEvent({ type: 'transaction', transaction: 'GET /' }); + client.captureEvent({ type: 'transaction', transaction: 'GET /_nuxt/some_asset.js' }); + // Although this has the name of the build asset directory (_nuxt), it should not be filtered out as it would not match the regex + client.captureEvent({ type: 'transaction', transaction: 'GET _nuxt/some_asset.js' }); + client.captureEvent({ type: 'transaction', transaction: 'POST /_server' }); + + await client!.flush(); + + expect(beforeSendEvent).toHaveBeenCalledTimes(3); + expect(beforeSendEvent).toHaveBeenCalledWith( + expect.objectContaining({ + transaction: 'GET /', + }), + expect.any(Object), + ); + expect(beforeSendEvent).toHaveBeenCalledWith( + expect.objectContaining({ + transaction: 'GET _nuxt/some_asset.js', + }), + expect.any(Object), + ); + expect(beforeSendEvent).not.toHaveBeenCalledWith( + expect.objectContaining({ + transaction: 'GET /_nuxt/some_asset.js', + }), + expect.any(Object), + ); + expect(beforeSendEvent).toHaveBeenCalledWith( + expect.objectContaining({ + transaction: 'POST /_server', + }), + expect.any(Object), + ); + }); }); });