Skip to content

Commit 09c5fad

Browse files
committed
test(loader): Improve loader tests & update loader
1 parent c9aaf8b commit 09c5fad

File tree

17 files changed

+73
-40
lines changed

17 files changed

+73
-40
lines changed

packages/browser-integration-tests/.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ module.exports = {
1313
'fixtures/**',
1414
'tmp/**',
1515
],
16+
overrides: [
17+
{
18+
files: ['loader-suites/**/{subject,init}.js'],
19+
globals: {
20+
Sentry: true,
21+
},
22+
},
23+
],
1624
parserOptions: {
1725
sourceType: 'module',
1826
},

packages/browser-integration-tests/fixtures/loader.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
import * as Sentry from '@sentry/browser';
21

3-
window.Sentry = Sentry;
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
41
window._testBaseTimestamp = performance.timeOrigin / 1000;
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
import * as Sentry from '@sentry/browser';
21

3-
window.Sentry = Sentry;

packages/browser-integration-tests/loader-suites/loader/noOnLoad/sdkLoadedInMeanwhile/subject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ setTimeout(() => {
33
cdnScript.src = '/cdn.bundle.js';
44

55
cdnScript.addEventListener('load', () => {
6-
window.Sentry.init({
6+
Sentry.init({
77
dsn: 'https://[email protected]/1337',
88
replaysSessionSampleRate: 0.42,
99
});
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
4-
51
Sentry.onLoad(function () {
2+
Sentry.init();
63
Sentry.captureException('Test exception');
74
});

packages/browser-integration-tests/loader-suites/loader/onLoad/captureExceptionInOnLoad/test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { sentryTest } from '../../../../utils/fixtures';
44
import { envelopeRequestParser, waitForErrorRequestOnUrl } from '../../../../utils/helpers';
55

66
sentryTest('captureException works inside of onLoad', async ({ getLocalTestUrl, page }) => {
7+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
8+
return route.fulfill({
9+
status: 200,
10+
contentType: 'application/json',
11+
body: JSON.stringify({ id: 'test-id' }),
12+
});
13+
});
14+
715
const url = await getLocalTestUrl({ testDir: __dirname });
816
const req = await waitForErrorRequestOnUrl(page, url);
917

packages/browser-integration-tests/loader-suites/loader/onLoad/customBrowserTracing/init.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
41
window._testBaseTimestamp = performance.timeOrigin / 1000;
52

63
Sentry.onLoad(function () {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
window.__sentryOnLoad = 0;
2+
3+
setTimeout(() => {
4+
Sentry.onLoad(function () {
5+
window.__hadSentry = window.sentryIsLoaded();
6+
7+
Sentry.init({
8+
sampleRate: 0.5,
9+
});
10+
11+
window.__sentryOnLoad++;
12+
});
13+
});
14+
15+
window.sentryIsLoaded = () => {
16+
const __sentry = window.__SENTRY__;
17+
18+
// If there is a global __SENTRY__ that means that in any of the callbacks init() was already invoked
19+
return !!(!(typeof __sentry === 'undefined') && __sentry.hub && __sentry.hub.getClient());
20+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { LOADER_CONFIGS } from '../../../../utils/generatePlugin';
5+
6+
const bundle = process.env.PW_BUNDLE || '';
7+
const isLazy = LOADER_CONFIGS[bundle]?.lazy;
8+
9+
sentryTest('always calls onLoad init correctly', async ({ getLocalTestUrl, page }) => {
10+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
11+
return route.fulfill({
12+
status: 200,
13+
contentType: 'application/json',
14+
body: JSON.stringify({ id: 'test-id' }),
15+
});
16+
});
17+
18+
const url = await getLocalTestUrl({ testDir: __dirname });
19+
20+
await page.goto(url);
21+
22+
// We want to test that if we are _not_ lazy, we are correctly calling onLoad init()
23+
// But if we are lazy and call `forceLoad`, we also call the onLoad init() correctly
24+
if (isLazy) {
25+
expect(await page.evaluate('window.__sentryOnLoad')).toEqual(0);
26+
await page.evaluate('Sentry.forceLoad()');
27+
}
28+
29+
await page.waitForFunction('window.__sentryOnLoad && window.sentryIsLoaded()');
30+
31+
expect(await page.evaluate('window.__hadSentry')).toEqual(false);
32+
expect(await page.evaluate('window.__sentryOnLoad')).toEqual(1);
33+
expect(await page.evaluate('Sentry.getCurrentHub().getClient().getOptions().sampleRate')).toEqual(0.5);
34+
});

packages/browser-integration-tests/loader-suites/loader/onLoad/customIntegrations/init.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
4-
51
class CustomIntegration {
62
constructor() {
73
this.name = 'CustomIntegration';

packages/browser-integration-tests/loader-suites/loader/onLoad/customIntegrationsFunction/init.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
4-
51
class CustomIntegration {
62
constructor() {
73
this.name = 'CustomIntegration';

packages/browser-integration-tests/loader-suites/loader/onLoad/customReplay/init.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
4-
51
Sentry.onLoad(function () {
62
Sentry.init({
73
integrations: [
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
4-
51
Sentry.onLoad(function () {
62
Sentry.init({});
73
});

packages/browser-integration-tests/loader-suites/loader/onLoad/pageloadTransaction/init.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
41
window._testBaseTimestamp = performance.timeOrigin / 1000;
52

63
Sentry.onLoad(function () {
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
4-
51
Sentry.onLoad(function () {
62
Sentry.init({});
73
});

0 commit comments

Comments
 (0)