Skip to content

Commit babbed8

Browse files
committed
ref(test): Trigger top-level errors inside sandbox.
1 parent 9a7e276 commit babbed8

File tree

18 files changed

+117
-133
lines changed

18 files changed

+117
-133
lines changed

dev-packages/browser-integration-tests/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ or `init.js` is not defined in a case folder.
1414

1515
`subject.js` contains the logic that sets up the environment to be tested. It also can be defined locally and as a group
1616
fallback. Unlike `template.hbs` and `init.js`, it's not required to be defined for a group, as there may be cases that
17-
does not require a subject, instead the logic is injected using `injectScriptAndGetEvents` from `utils/helpers.ts`.
17+
does not require a subject.
1818

1919
`test.ts` is required for each test case, which contains the assertions (and if required the script injection logic).
2020
For every case, any set of `init.js`, `template.hbs` and `subject.js` can be defined locally, and each one of them will
2121
have precedence over the default definitions of the test group.
2222

2323
To test page multi-page navigations, you can specify additional `page-*.html` (e.g. `page-0.html`, `page-1.html`) files.
2424
These will also be compiled and initialized with the same `init.js` and `subject.js` files that are applied to
25-
`template.hbs/html`. Note: `page-*.html` file lookup **doesn not** fall back to the parent directories, meaning that
26-
page files have to be directly in the `test.ts` directory.
25+
`template.hbs/html`. Note: `page-*.html` file lookup **does not** fall back to the parent directories, meaning that page
26+
files have to be directly in the `test.ts` directory.
2727

2828
```
2929
suites/

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/non-string-arg/subject.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/non-string-arg/test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33

44
import { sentryTest } from '../../../../../utils/fixtures';
5-
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers';
5+
import { getFirstSentryEnvelopeRequest, runScriptInSandbox } from '../../../../../utils/helpers';
66

77
sentryTest(
88
'should catch onerror calls with non-string first argument gracefully',
99
async ({ getLocalTestPath, page }) => {
1010
const url = await getLocalTestPath({ testDir: __dirname });
1111

12-
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
12+
await page.goto(url);
13+
14+
runScriptInSandbox(page, {
15+
content: `
16+
throw {
17+
type: 'Error',
18+
otherKey: 'otherValue',
19+
};
20+
`,
21+
});
22+
23+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page);
1324

1425
expect(eventData.exception?.values).toHaveLength(1);
1526
expect(eventData.exception?.values?.[0]).toMatchObject({

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/rethrown/subject.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/rethrown/test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@ import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33

44
import { sentryTest } from '../../../../../utils/fixtures';
5-
import { getMultipleSentryEnvelopeRequests } from '../../../../../utils/helpers';
5+
import { getMultipleSentryEnvelopeRequests, runScriptInSandbox } from '../../../../../utils/helpers';
66

77
sentryTest(
88
'should NOT catch an exception already caught [but rethrown] via Sentry.captureException',
99
async ({ getLocalTestPath, page }) => {
1010
const url = await getLocalTestPath({ testDir: __dirname });
1111

12-
const events = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url });
12+
await page.goto(url);
13+
14+
runScriptInSandbox(page, {
15+
content: `
16+
try {
17+
foo();
18+
} catch (e) {
19+
Sentry.captureException(e);
20+
throw e;
21+
}
22+
`,
23+
});
24+
25+
const events = await getMultipleSentryEnvelopeRequests<Event>(page, 1);
1326

1427
expect(events[0].exception?.values).toHaveLength(1);
1528
expect(events[0].exception?.values?.[0]).toMatchObject({
@@ -24,19 +37,5 @@ sentryTest(
2437
frames: expect.any(Array),
2538
},
2639
});
27-
28-
// This is not a refernece error, but another generic error
29-
expect(events[1].exception?.values).toHaveLength(1);
30-
expect(events[1].exception?.values?.[0]).toMatchObject({
31-
type: 'Error',
32-
value: 'error 2',
33-
mechanism: {
34-
type: 'generic',
35-
handled: true,
36-
},
37-
stacktrace: {
38-
frames: expect.any(Array),
39-
},
40-
});
4140
},
4241
);

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/syntax-errors/subject.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/syntax-errors/test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33

44
import { sentryTest } from '../../../../../utils/fixtures';
5-
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers';
5+
import { getFirstSentryEnvelopeRequest, runScriptInSandbox } from '../../../../../utils/helpers';
66

77
sentryTest('should catch syntax errors', async ({ getLocalTestPath, page }) => {
88
const url = await getLocalTestPath({ testDir: __dirname });
99

10-
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
10+
await page.goto(url);
11+
12+
runScriptInSandbox(page, {
13+
content: `
14+
foo{}; // SyntaxError
15+
`,
16+
});
17+
18+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page);
1119

1220
expect(eventData.exception?.values).toHaveLength(1);
1321
expect(eventData.exception?.values?.[0]).toMatchObject({

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/thrown-errors/subject.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/thrown-errors/test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33

44
import { sentryTest } from '../../../../../utils/fixtures';
5-
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers';
5+
import { getFirstSentryEnvelopeRequest, runScriptInSandbox } from '../../../../../utils/helpers';
66

77
sentryTest('should catch thrown errors', async ({ getLocalTestPath, page }) => {
88
const url = await getLocalTestPath({ testDir: __dirname });
99

10-
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
10+
await page.goto(url);
11+
12+
runScriptInSandbox(page, {
13+
content: `
14+
throw new Error('realError');
15+
`,
16+
});
17+
18+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page);
1119

1220
expect(eventData.exception?.values).toHaveLength(1);
1321
expect(eventData.exception?.values?.[0]).toMatchObject({

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/thrown-objects/subject.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/thrown-objects/test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33

44
import { sentryTest } from '../../../../../utils/fixtures';
5-
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers';
5+
import { getFirstSentryEnvelopeRequest, runScriptInSandbox } from '../../../../../utils/helpers';
66

77
sentryTest('should catch thrown objects', async ({ getLocalTestPath, page }) => {
88
const url = await getLocalTestPath({ testDir: __dirname });
99

10-
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
10+
await page.goto(url);
11+
12+
runScriptInSandbox(page, {
13+
content: `
14+
throw {
15+
error: 'stuff is broken',
16+
somekey: 'ok'
17+
};`,
18+
});
19+
20+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page);
1121

1222
expect(eventData.exception?.values).toHaveLength(1);
1323
expect(eventData.exception?.values?.[0]).toMatchObject({

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/thrown-strings/subject.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/public-api/instrumentation/onError/thrown-strings/test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33

44
import { sentryTest } from '../../../../../utils/fixtures';
5-
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers';
5+
import { getFirstSentryEnvelopeRequest, runScriptInSandbox } from '../../../../../utils/helpers';
66

77
sentryTest('should catch thrown strings', async ({ getLocalTestPath, page }) => {
88
const url = await getLocalTestPath({ testDir: __dirname });
99

10-
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
10+
await page.goto(url);
11+
12+
runScriptInSandbox(page, {
13+
content: `
14+
throw 'stringError';
15+
`,
16+
});
17+
18+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page);
1119

1220
expect(eventData.exception?.values).toHaveLength(1);
1321
expect(eventData.exception?.values?.[0]).toMatchObject({

dev-packages/browser-integration-tests/suites/public-api/startSpan/error-sync/subject.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/public-api/startSpan/error-sync/test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,34 @@ import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33

44
import { sentryTest } from '../../../../utils/fixtures';
5-
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers';
5+
import {
6+
getMultipleSentryEnvelopeRequests,
7+
runScriptInSandbox,
8+
shouldSkipTracingTest,
9+
} from '../../../../utils/helpers';
610

711
sentryTest('should capture an error within a sync startSpan callback', async ({ getLocalTestPath, page }) => {
812
if (shouldSkipTracingTest()) {
913
sentryTest.skip();
1014
}
1115

1216
const url = await getLocalTestPath({ testDir: __dirname });
13-
const gotoPromise = page.goto(url);
14-
const envelopePromise = getMultipleSentryEnvelopeRequests<Event>(page, 2);
17+
await page.goto(url);
18+
19+
runScriptInSandbox(page, {
20+
content: `
21+
function run() {
22+
Sentry.startSpan({ name: 'parent_span' }, () => {
23+
throw new Error('Sync Error');
24+
});
25+
}
26+
27+
setTimeout(run);
28+
`,
29+
});
30+
31+
const events = await getMultipleSentryEnvelopeRequests<Event>(page, 2);
1532

16-
const [, events] = await Promise.all([gotoPromise, envelopePromise]);
1733
const txn = events.find(event => event.type === 'transaction');
1834
const err = events.find(event => !event.type);
1935

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/error/subject.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/error/test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { expect } from '@playwright/test';
22
import type { Event } from '@sentry/types';
33
import { sentryTest } from '../../../../utils/fixtures';
4-
import { getMultipleSentryEnvelopeRequests, shouldSkipTracingTest } from '../../../../utils/helpers';
4+
import {
5+
getMultipleSentryEnvelopeRequests,
6+
runScriptInSandbox,
7+
shouldSkipTracingTest,
8+
} from '../../../../utils/helpers';
59

610
sentryTest(
711
'should put the pageload transaction name onto an error event caught during pageload',
@@ -14,6 +18,12 @@ sentryTest(
1418

1519
await page.goto(url);
1620

21+
runScriptInSandbox(page, {
22+
content: `
23+
throw new Error('Error during pageload');
24+
`,
25+
});
26+
1727
const [e1, e2] = await getMultipleSentryEnvelopeRequests<Event>(page, 2);
1828

1929
const pageloadTxnEvent = e1.type === 'transaction' ? e1 : e2;

dev-packages/browser-integration-tests/utils/helpers.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,17 @@ export const countEnvelopes = async (
141141
* Run script at the given path inside the test environment.
142142
*
143143
* @param {Page} page
144-
* @param {string} path
144+
* @param {{ path?: string; content?: string }} impl
145145
* @return {*} {Promise<void>}
146146
*/
147-
async function runScriptInSandbox(page: Page, path: string): Promise<void> {
148-
await page.addScriptTag({ path });
147+
async function runScriptInSandbox(
148+
page: Page,
149+
impl: {
150+
path?: string;
151+
content?: string;
152+
},
153+
): Promise<void> {
154+
await page.addScriptTag({ path: impl.path, content: impl.content });
149155
}
150156

151157
/**
@@ -333,26 +339,11 @@ async function getFirstSentryEnvelopeRequest<T>(
333339
}
334340

335341
/**
336-
* Manually inject a script into the page of given URL.
337-
* This function is useful to create more complex test subjects that can't be achieved by pre-built pages.
338-
* The given script should be vanilla browser JavaScript
342+
* Trigger an error in the page context.
343+
* This function is useful to test error handling in the page.
339344
*
340345
* @param {Page} page
341-
* @param {string} url
342-
* @param {string} scriptPath
343-
* @return {*} {Promise<Array<Event>>}
346+
* @param {unknown} error
344347
*/
345-
async function injectScriptAndGetEvents(page: Page, url: string, scriptPath: string): Promise<Array<Event>> {
346-
await page.goto(url);
347-
await runScriptInSandbox(page, scriptPath);
348348

349-
return getSentryEvents(page);
350-
}
351-
352-
export {
353-
runScriptInSandbox,
354-
getMultipleSentryEnvelopeRequests,
355-
getFirstSentryEnvelopeRequest,
356-
getSentryEvents,
357-
injectScriptAndGetEvents,
358-
};
349+
export { runScriptInSandbox, getMultipleSentryEnvelopeRequests, getFirstSentryEnvelopeRequest, getSentryEvents };

0 commit comments

Comments
 (0)