From 091d23b7a9b31786a12128757b3792d2bac03121 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 8 Apr 2024 10:15:12 -0400 Subject: [PATCH] test: Port ignoreErrors and denyUrls tests from karma runner (#11449) I want to remove the karma/mocha based tests in the browser package. To accomplish this, I'll be porting 1 test suite a day from the old integration tests to playwright. Today is Day 3: `packages/browser/test/integration/suites/config.js` I was surprised we never had `ignoreErrors` or `denyUrls` tests in playwright, so it's good to get the confidence that everything works here. ref https://github.com/getsentry/sentry-javascript/issues/11084 day 2: https://github.com/getsentry/sentry-javascript/pull/11436 --- .../suites/public-api/denyUrls/init.js | 14 +++++ .../suites/public-api/denyUrls/subject.js | 32 +++++++++++ .../suites/public-api/denyUrls/test.ts | 17 ++++++ .../suites/public-api/ignoreErrors/init.js | 14 +++++ .../suites/public-api/ignoreErrors/subject.js | 3 + .../suites/public-api/ignoreErrors/test.ts | 19 +++++++ .../browser/test/integration/suites/config.js | 55 ------------------- .../browser/test/integration/suites/shell.js | 1 - 8 files changed, 99 insertions(+), 56 deletions(-) create mode 100644 dev-packages/browser-integration-tests/suites/public-api/denyUrls/init.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/denyUrls/subject.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/denyUrls/test.ts create mode 100644 dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/init.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/subject.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/test.ts delete mode 100644 packages/browser/test/integration/suites/config.js diff --git a/dev-packages/browser-integration-tests/suites/public-api/denyUrls/init.js b/dev-packages/browser-integration-tests/suites/public-api/denyUrls/init.js new file mode 100644 index 000000000000..c16b31fd1c85 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/denyUrls/init.js @@ -0,0 +1,14 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +window._errorCount = 0; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + denyUrls: ['foo.js'], + beforeSend: event => { + window._errorCount++; + return event; + }, +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/denyUrls/subject.js b/dev-packages/browser-integration-tests/suites/public-api/denyUrls/subject.js new file mode 100644 index 000000000000..d0189ca3db75 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/denyUrls/subject.js @@ -0,0 +1,32 @@ +/** + * We always filter on the caller, not the cause of the error + * + * > foo.js file called a function in bar.js + * > bar.js file called a function in baz.js + * > baz.js threw an error + * + * foo.js is denied in the `init` call (init.js), thus we filter it + * */ +var urlWithDeniedUrl = new Error('filter'); +urlWithDeniedUrl.stack = + 'Error: bar\n' + + ' at http://localhost:5000/foo.js:7:19\n' + + ' at bar(http://localhost:5000/bar.js:2:3)\n' + + ' at baz(http://localhost:5000/baz.js:2:9)\n'; + +/** + * > foo-pass.js file called a function in bar-pass.js + * > bar-pass.js file called a function in baz-pass.js + * > baz-pass.js threw an error + * + * foo-pass.js is *not* denied in the `init` call (init.js), thus we don't filter it + * */ +var urlWithoutDeniedUrl = new Error('pass'); +urlWithoutDeniedUrl.stack = + 'Error: bar\n' + + ' at http://localhost:5000/foo-pass.js:7:19\n' + + ' at bar(http://localhost:5000/bar-pass.js:2:3)\n' + + ' at baz(http://localhost:5000/baz-pass.js:2:9)\n'; + +Sentry.captureException(urlWithDeniedUrl); +Sentry.captureException(urlWithoutDeniedUrl); diff --git a/dev-packages/browser-integration-tests/suites/public-api/denyUrls/test.ts b/dev-packages/browser-integration-tests/suites/public-api/denyUrls/test.ts new file mode 100644 index 000000000000..c374e8ae766c --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/denyUrls/test.ts @@ -0,0 +1,17 @@ +import { expect } from '@playwright/test'; +import type { Event } from '@sentry/types'; + +import { sentryTest } from '../../../utils/fixtures'; +import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers'; + +sentryTest('should allow to ignore specific urls', async ({ getLocalTestPath, page }) => { + const url = await getLocalTestPath({ testDir: __dirname }); + + const eventData = await getFirstSentryEnvelopeRequest(page, url); + + expect(eventData.exception?.values?.[0].type).toEqual('Error'); + expect(eventData.exception?.values?.[0].value).toEqual('pass'); + + const count = await page.evaluate('window._errorCount'); + expect(count).toEqual(1); +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/init.js b/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/init.js new file mode 100644 index 000000000000..e66214c5c0bf --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/init.js @@ -0,0 +1,14 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +window._errorCount = 0; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + ignoreErrors: ['ignoreErrorTest'], + beforeSend: event => { + window._errorCount++; + return event; + }, +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/subject.js b/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/subject.js new file mode 100644 index 000000000000..9f7883c97284 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/subject.js @@ -0,0 +1,3 @@ +Sentry.captureException(new Error('foo')); +Sentry.captureException(new Error('ignoreErrorTest')); +Sentry.captureException(new Error('bar')); diff --git a/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/test.ts b/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/test.ts new file mode 100644 index 000000000000..35752ae39232 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/ignoreErrors/test.ts @@ -0,0 +1,19 @@ +import { expect } from '@playwright/test'; +import type { Event } from '@sentry/types'; + +import { sentryTest } from '../../../utils/fixtures'; +import { getMultipleSentryEnvelopeRequests } from '../../../utils/helpers'; + +sentryTest('should allow to ignore specific errors', async ({ getLocalTestPath, page }) => { + const url = await getLocalTestPath({ testDir: __dirname }); + + const events = await getMultipleSentryEnvelopeRequests(page, 2, { url }); + + expect(events[0].exception?.values?.[0].type).toEqual('Error'); + expect(events[0].exception?.values?.[0].value).toEqual('foo'); + expect(events[1].exception?.values?.[0].type).toEqual('Error'); + expect(events[1].exception?.values?.[0].value).toEqual('bar'); + + const count = await page.evaluate('window._errorCount'); + expect(count).toEqual(2); +}); diff --git a/packages/browser/test/integration/suites/config.js b/packages/browser/test/integration/suites/config.js deleted file mode 100644 index 2637ea8c13b7..000000000000 --- a/packages/browser/test/integration/suites/config.js +++ /dev/null @@ -1,55 +0,0 @@ -describe('config', function () { - it('should allow to ignore specific errors', function () { - return runInSandbox(sandbox, function () { - Sentry.captureException(new Error('foo')); - Sentry.captureException(new Error('ignoreErrorTest')); - Sentry.captureException(new Error('bar')); - }).then(function (summary) { - assert.equal(summary.events[0].exception.values[0].type, 'Error'); - assert.equal(summary.events[0].exception.values[0].value, 'foo'); - assert.equal(summary.events[1].exception.values[0].type, 'Error'); - assert.equal(summary.events[1].exception.values[0].value, 'bar'); - }); - }); - - it('should allow to ignore specific urls', function () { - return runInSandbox(sandbox, function () { - /** - * We always filter on the caller, not the cause of the error - * - * > foo.js file called a function in bar.js - * > bar.js file called a function in baz.js - * > baz.js threw an error - * - * foo.js is denied in the `init` call (init.js), thus we filter it - * */ - var urlWithDeniedUrl = new Error('filter'); - urlWithDeniedUrl.stack = - 'Error: bar\n' + - ' at http://localhost:5000/foo.js:7:19\n' + - ' at bar(http://localhost:5000/bar.js:2:3)\n' + - ' at baz(http://localhost:5000/baz.js:2:9)\n'; - - /** - * > foo-pass.js file called a function in bar-pass.js - * > bar-pass.js file called a function in baz-pass.js - * > baz-pass.js threw an error - * - * foo-pass.js is *not* denied in the `init` call (init.js), thus we don't filter it - * */ - var urlWithoutDeniedUrl = new Error('pass'); - urlWithoutDeniedUrl.stack = - 'Error: bar\n' + - ' at http://localhost:5000/foo-pass.js:7:19\n' + - ' at bar(http://localhost:5000/bar-pass.js:2:3)\n' + - ' at baz(http://localhost:5000/baz-pass.js:2:9)\n'; - - Sentry.captureException(urlWithDeniedUrl); - Sentry.captureException(urlWithoutDeniedUrl); - }).then(function (summary) { - assert.lengthOf(summary.events, 1); - assert.equal(summary.events[0].exception.values[0].type, 'Error'); - assert.equal(summary.events[0].exception.values[0].value, 'pass'); - }); - }); -}); diff --git a/packages/browser/test/integration/suites/shell.js b/packages/browser/test/integration/suites/shell.js index e1555623b495..7df81ae2b53e 100644 --- a/packages/browser/test/integration/suites/shell.js +++ b/packages/browser/test/integration/suites/shell.js @@ -22,7 +22,6 @@ function runVariant(variant) { /** * The test runner will replace each of these placeholders with the contents of the corresponding file. */ - {{ suites/config.js }} // biome-ignore format: No trailing commas {{ suites/onerror.js }} // biome-ignore format: No trailing commas {{ suites/onunhandledrejection.js }} // biome-ignore format: No trailing commas {{ suites/builtins.js }} // biome-ignore format: No trailing commas