From 855cb08d708a08dcbb9a111afa6d17fa03e1f2a6 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 23 Oct 2023 11:30:01 +0200 Subject: [PATCH] test(loader): Add tests for loader `window.sentryOnLoad` --- .../browser-integration-tests/fixtures/loader.js | 2 +- .../loader/onLoad/sentryOnLoad/init.js | 1 + .../loader/onLoad/sentryOnLoad/subject.js | 1 + .../loader/onLoad/sentryOnLoad/template.html | 14 ++++++++++++++ .../loader/onLoad/sentryOnLoad/test.ts | 15 +++++++++++++++ .../loader/onLoad/sentryOnLoadAndOnLoad/init.js | 4 ++++ .../onLoad/sentryOnLoadAndOnLoad/subject.js | 1 + .../onLoad/sentryOnLoadAndOnLoad/template.html | 14 ++++++++++++++ .../loader/onLoad/sentryOnLoadAndOnLoad/test.ts | 15 +++++++++++++++ 9 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/init.js create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/subject.js create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/template.html create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/test.ts create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/init.js create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/subject.js create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/template.html create mode 100644 packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/test.ts diff --git a/packages/browser-integration-tests/fixtures/loader.js b/packages/browser-integration-tests/fixtures/loader.js index a6fa24465a4f..9387f3982997 100644 --- a/packages/browser-integration-tests/fixtures/loader.js +++ b/packages/browser-integration-tests/fixtures/loader.js @@ -1,4 +1,4 @@ -!function(n,e,r,t,i,o,a,c,u){for(var s=u,f=0;f-1){s&&"no"===document.scripts[f].getAttribute("data-lazy")&&(s=!1);break}var p=[];function l(n){return"e"in n}function d(n){return"p"in n}function _(n){return"f"in n}var v=[];function h(n){s&&(l(n)||d(n)||_(n)&&n.f.indexOf("capture")>-1||_(n)&&n.f.indexOf("showReportDialog")>-1)&&O(),v.push(n)}function y(){h({e:[].slice.call(arguments)})}function E(n){h({p:"reason"in n?n.reason:"detail"in n&&"reason"in n.detail?n.detail.reason:n})}function m(){try{n.SENTRY_SDK_SOURCE="loader";var e=n[i],o=e.init;e.init=function(i){n.removeEventListener(r,y),n.removeEventListener(t,E);var a=c;for(var u in i)Object.prototype.hasOwnProperty.call(i,u)&&(a[u]=i[u]);!function(n,e){var r=n.integrations||[];if(!Array.isArray(r))return;var t=r.map((function(n){return n.name}));n.tracesSampleRate&&-1===t.indexOf("BrowserTracing")&&r.push(new e.BrowserTracing);(n.replaysSessionSampleRate||n.replaysOnErrorSampleRate)&&-1===t.indexOf("Replay")&&r.push(new e.Replay);n.integrations=r}(a,e),o(a)},setTimeout((function(){return function(e){try{for(var r=0;r-1){u&&"no"===document.scripts[f].getAttribute("data-lazy")&&(u=!1);break}var p=[];function d(n){return"e"in n}function l(n){return"p"in n}function _(n){return"f"in n}var v=[];function y(n){u&&(d(n)||l(n)||_(n)&&n.f.indexOf("capture")>-1||_(n)&&n.f.indexOf("showReportDialog")>-1)&&L(),v.push(n)}function h(){y({e:[].slice.call(arguments)})}function E(n){y({p:"reason"in n?n.reason:"detail"in n&&"reason"in n.detail?n.detail.reason:n})}function O(){try{n.SENTRY_SDK_SOURCE="loader";var e=n[i],o=e.init;e.init=function(i){n.removeEventListener(t,h),n.removeEventListener(r,E);var a=c;for(var s in i)Object.prototype.hasOwnProperty.call(i,s)&&(a[s]=i[s]);!function(n,e){var t=n.integrations||[];if(!Array.isArray(t))return;var r=t.map((function(n){return n.name}));n.tracesSampleRate&&-1===r.indexOf("BrowserTracing")&&t.push(new e.BrowserTracing);(n.replaysSessionSampleRate||n.replaysOnErrorSampleRate)&&-1===r.indexOf("Replay")&&t.push(new e.Replay);n.integrations=t}(a,e),o(a)},setTimeout((function(){return function(e){try{"function"==typeof n.sentryOnLoad&&(n.sentryOnLoad(),n.sentryOnLoad=void 0);for(var t=0;t + + + + + + + diff --git a/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/test.ts b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/test.ts new file mode 100644 index 000000000000..1a08f07b4abb --- /dev/null +++ b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoad/test.ts @@ -0,0 +1,15 @@ +import { expect } from '@playwright/test'; + +import { sentryTest } from '../../../../utils/fixtures'; +import { envelopeRequestParser, waitForErrorRequestOnUrl } from '../../../../utils/helpers'; + +sentryTest('sentryOnLoad callback is called before Sentry.onLoad()', async ({ getLocalTestUrl, page }) => { + const url = await getLocalTestUrl({ testDir: __dirname }); + const req = await waitForErrorRequestOnUrl(page, url); + + const eventData = envelopeRequestParser(req); + + expect(eventData.message).toBe('Test exception'); + + expect(await page.evaluate('Sentry.getCurrentHub().getClient().getOptions().tracesSampleRate')).toEqual(0.123); +}); diff --git a/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/init.js b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/init.js new file mode 100644 index 000000000000..c922dae2569a --- /dev/null +++ b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/init.js @@ -0,0 +1,4 @@ +Sentry.onLoad(function () { + // this should be called _after_ window.sentryOnLoad + Sentry.captureException(`Test exception: ${Sentry.getCurrentHub().getClient().getOptions().tracesSampleRate}`); +}); diff --git a/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/subject.js b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/subject.js new file mode 100644 index 000000000000..fb0796f7f299 --- /dev/null +++ b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/subject.js @@ -0,0 +1 @@ +Sentry.captureException('Test exception'); diff --git a/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/template.html b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/template.html new file mode 100644 index 000000000000..ed23488f1bf3 --- /dev/null +++ b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/template.html @@ -0,0 +1,14 @@ + + + + + + + + diff --git a/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/test.ts b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/test.ts new file mode 100644 index 000000000000..00a72b7613b7 --- /dev/null +++ b/packages/browser-integration-tests/loader-suites/loader/onLoad/sentryOnLoadAndOnLoad/test.ts @@ -0,0 +1,15 @@ +import { expect } from '@playwright/test'; + +import { sentryTest } from '../../../../utils/fixtures'; +import { envelopeRequestParser, waitForErrorRequestOnUrl } from '../../../../utils/helpers'; + +sentryTest('sentryOnLoad callback is used', async ({ getLocalTestUrl, page }) => { + const url = await getLocalTestUrl({ testDir: __dirname }); + const req = await waitForErrorRequestOnUrl(page, url); + + const eventData = envelopeRequestParser(req); + + expect(eventData.message).toBe('Test exception: 0.123'); + + expect(await page.evaluate('Sentry.getCurrentHub().getClient().getOptions().tracesSampleRate')).toEqual(0.123); +});