Skip to content

Commit 8d5c306

Browse files
authored
test(loader): Improve loader tests & update loader (#9245)
This updates the loader based on this PR: getsentry/sentry#58070 and also actually fixes/improves the loader tests by avoiding importing `@sentry/browser`. This is not needed and actually obfuscates the "real"/"normal" behavior (just look at the generated `dist` folder for a test. Instead, I added an eslint rule to allow `Sentry` as a global for these cases, and just access them directly. This also uncovered an incorrect test, which I adjusted so it works. I also added a new test to ensure custom config works.
1 parent aea3905 commit 8d5c306

File tree

21 files changed

+103
-39
lines changed

21 files changed

+103
-39
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.md
22
.nxcache
3+
packages/browser-integration-tests/fixtures/loader.js

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 & 1 deletion
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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import * as Sentry from '@sentry/browser';
2-
3-
window.Sentry = Sentry;
4-
51
Sentry.onLoad(function () {
2+
// You _have_ to call Sentry.init() before calling Sentry.captureException() in Sentry.onLoad()!
3+
Sentry.init();
64
Sentry.captureException('Test exception');
75
});

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/onLoadLate/init.js

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Sentry.forceLoad();
2+
3+
setTimeout(() => {
4+
Sentry.onLoad(function () {
5+
Sentry.captureException('Test exception');
6+
});
7+
}, 200);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { envelopeRequestParser, waitForErrorRequestOnUrl } from '../../../../utils/helpers';
5+
6+
sentryTest('late onLoad call is handled', 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+
15+
const url = await getLocalTestUrl({ testDir: __dirname });
16+
const req = await waitForErrorRequestOnUrl(page, url);
17+
18+
const eventData = envelopeRequestParser(req);
19+
20+
expect(eventData.message).toBe('Test exception');
21+
});

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)