Skip to content

Commit

Permalink
test(loader): Improve loader tests & update loader
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Oct 13, 2023
1 parent 3f4d823 commit d29be99
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 40 deletions.
8 changes: 8 additions & 0 deletions packages/browser-integration-tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ module.exports = {
'fixtures/**',
'tmp/**',
],
overrides: [
{
files: ['loader-suites/**/{subject,init}.js'],
globals: {
Sentry: true,
},
},
],
parserOptions: {
sourceType: 'module',
},
Expand Down
3 changes: 1 addition & 2 deletions packages/browser-integration-tests/fixtures/loader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
window._testBaseTimestamp = performance.timeOrigin / 1000;
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ setTimeout(() => {
cdnScript.src = '/cdn.bundle.js';

cdnScript.addEventListener('load', () => {
window.Sentry.init({
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
replaysSessionSampleRate: 0.42,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.onLoad(function () {
Sentry.init();
Sentry.captureException('Test exception');
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { sentryTest } from '../../../../utils/fixtures';
import { envelopeRequestParser, waitForErrorRequestOnUrl } from '../../../../utils/helpers';

sentryTest('captureException works inside of onLoad', async ({ getLocalTestUrl, page }) => {
page.on('console', msg => console.log(msg.text()));

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname });
const req = await waitForErrorRequestOnUrl(page, url);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
window._testBaseTimestamp = performance.timeOrigin / 1000;

Sentry.onLoad(function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
window.__sentryOnLoad = 0;

setTimeout(() => {
Sentry.onLoad(function () {
window.__hadSentry = window.sentryIsLoaded();

Sentry.init({
sampleRate: 0.5,
});

window.__sentryOnLoad++;
});
});

window.sentryIsLoaded = () => {
const __sentry = window.__SENTRY__;

// If there is a global __SENTRY__ that means that in any of the callbacks init() was already invoked
return !!(!(typeof __sentry === 'undefined') && __sentry.hub && __sentry.hub.getClient());
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';
import { LOADER_CONFIGS } from '../../../../utils/generatePlugin';

const bundle = process.env.PW_BUNDLE || '';
const isLazy = LOADER_CONFIGS[bundle]?.lazy;

sentryTest('always calls onLoad init correctly', async ({ getLocalTestUrl, page }) => {
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname });

await page.goto(url);

// We want to test that if we are _not_ lazy, we are correctly calling onLoad init()
// But if we are lazy and call `forceLoad`, we also call the onLoad init() correctly
if (isLazy) {
expect(await page.evaluate('window.__sentryOnLoad')).toEqual(0);
await page.evaluate('Sentry.forceLoad()');
}

await page.waitForFunction('window.__sentryOnLoad && window.sentryIsLoaded()');

expect(await page.evaluate('window.__hadSentry')).toEqual(false);
expect(await page.evaluate('window.__sentryOnLoad')).toEqual(1);
expect(await page.evaluate('Sentry.getCurrentHub().getClient().getOptions().sampleRate')).toEqual(0.5);
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

class CustomIntegration {
constructor() {
this.name = 'CustomIntegration';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

class CustomIntegration {
constructor() {
this.name = 'CustomIntegration';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.onLoad(function () {
Sentry.init({
integrations: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.onLoad(function () {
Sentry.init({});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
window._testBaseTimestamp = performance.timeOrigin / 1000;

Sentry.onLoad(function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.onLoad(function () {
Sentry.init({});
});

0 comments on commit d29be99

Please sign in to comment.