Skip to content

Commit e307608

Browse files
authored
test: Port captureException and captureMessage tests from karma runner (#11412)
I want to remove the karma/mocha based tests in the browser package. To accomplish this, I'll be porting 1 test suite a day from the old integration tests to playwright. Today is Day 1: `packages/browser/test/integration/suites/api.js` We have decent coverage around singular calls to these methods, so I just added the tests that validate calling `captureException` and `captureMessage` multiple times. I also fixed a spelling mistake with `dev-packages/browser-integration-tests/suites/public-api/captureException/linkedErrors/subject.js`, `linkedErrrors` -> `linkedErrors` ref #11084
1 parent 3790f1b commit e307608

File tree

11 files changed

+191
-163
lines changed

11 files changed

+191
-163
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function bar() {
2+
baz();
3+
}
4+
5+
function foo() {
6+
bar();
7+
}
8+
9+
function foo2() {
10+
// identical to foo, but meant for testing
11+
// different stack frame fns w/ same stack length
12+
bar();
13+
}
14+
// same error message, but different stacks means that these are considered
15+
// different errors
16+
17+
// stack:
18+
// bar
19+
try {
20+
bar();
21+
} catch (e) {
22+
Sentry.captureException(e);
23+
}
24+
25+
// stack (different # frames):
26+
// bar
27+
// foo
28+
try {
29+
foo();
30+
} catch (e) {
31+
Sentry.captureException(e);
32+
}
33+
34+
// stack (same # frames, different frames):
35+
// bar
36+
// foo2
37+
try {
38+
foo2();
39+
} catch (e) {
40+
Sentry.captureException(e);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getMultipleSentryEnvelopeRequests } from '../../../../utils/helpers';
6+
7+
sentryTest('should not reject back-to-back errors with different stack traces', async ({ getLocalTestPath, page }) => {
8+
const url = await getLocalTestPath({ testDir: __dirname });
9+
10+
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 3, { url });
11+
12+
// NOTE: regex because exact error message differs per-browser
13+
expect(eventData[0].exception?.values?.[0].value).toMatch(/baz/);
14+
expect(eventData[0].exception?.values?.[0].type).toMatch('ReferenceError');
15+
expect(eventData[1].exception?.values?.[0].value).toMatch(/baz/);
16+
expect(eventData[1].exception?.values?.[0].type).toMatch('ReferenceError');
17+
expect(eventData[2].exception?.values?.[0].value).toMatch(/baz/);
18+
expect(eventData[2].exception?.values?.[0].type).toMatch('ReferenceError');
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function throwError(message) {
2+
// eslint-disable-next-line no-param-reassign
3+
message = message || 'foo';
4+
try {
5+
throw new Error(message);
6+
} catch (o_O) {
7+
Sentry.captureException(o_O);
8+
}
9+
}
10+
11+
function throwRandomError() {
12+
try {
13+
throw new Error('Exception no ' + (Date.now() + Math.random()));
14+
} catch (o_O) {
15+
Sentry.captureException(o_O);
16+
}
17+
}
18+
19+
function createError() {
20+
function nestedFunction() {
21+
return new Error('created');
22+
}
23+
24+
return nestedFunction();
25+
}
26+
27+
function throwSameConsecutiveErrors(message) {
28+
throwError(message);
29+
throwError(message);
30+
}
31+
32+
// Different exceptions, don't dedupe
33+
for (var i = 0; i < 2; i++) {
34+
throwRandomError();
35+
}
36+
37+
// Same exceptions and same stacktrace, dedupe
38+
for (var j = 0; j < 2; j++) {
39+
throwError();
40+
}
41+
42+
const syntheticError = createError();
43+
44+
// Same exception, with transaction in between, dedupe
45+
Sentry.captureException(syntheticError);
46+
Sentry.captureEvent({
47+
event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2',
48+
message: 'someMessage',
49+
transaction: 'wat',
50+
type: 'transaction',
51+
});
52+
Sentry.captureException(syntheticError);
53+
54+
// Same exceptions, different stacktrace (different line number), don't dedupe
55+
throwSameConsecutiveErrors('bar');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getMultipleSentryEnvelopeRequests } from '../../../../utils/helpers';
6+
7+
sentryTest('should reject duplicate, back-to-back errors from captureException', async ({ getLocalTestPath, page }) => {
8+
const url = await getLocalTestPath({ testDir: __dirname });
9+
10+
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 7, { url });
11+
12+
// NOTE: regex because exact error message differs per-browser
13+
expect(eventData[0].exception?.values?.[0].value).toMatch(/Exception no \d+/);
14+
expect(eventData[1].exception?.values?.[0].value).toMatch(/Exception no \d+/);
15+
expect(eventData[2].exception?.values?.[0].value).toEqual('foo');
16+
// transaction so undefined
17+
expect(eventData[3].exception?.values?.[0].value).toEqual('created');
18+
expect(eventData[4].exception?.values?.[0].value).toEqual(undefined);
19+
expect(eventData[5].exception?.values?.[0].value).toEqual('bar');
20+
expect(eventData[6].exception?.values?.[0].value).toEqual('bar');
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
attachStacktrace: true,
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function captureMessage(message) {
2+
// eslint-disable-next-line no-param-reassign
3+
message = message || 'message';
4+
Sentry.captureMessage(message);
5+
}
6+
7+
function captureRandomMessage() {
8+
Sentry.captureMessage('Message no ' + (Date.now() + Math.random()));
9+
}
10+
11+
function captureSameConsecutiveMessages(message) {
12+
captureMessage(message);
13+
captureMessage(message);
14+
}
15+
16+
// Different messages, don't dedupe
17+
for (var i = 0; i < 2; i++) {
18+
captureRandomMessage();
19+
}
20+
21+
// Same messages and same stacktrace, dedupe
22+
for (var j = 0; j < 3; j++) {
23+
captureMessage('same message, same stacktrace');
24+
}
25+
26+
// Same messages, different stacktrace (different line number), don't dedupe
27+
captureSameConsecutiveMessages('same message, different stacktrace');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { getMultipleSentryEnvelopeRequests } from '../../../../utils/helpers';
6+
7+
sentryTest(
8+
'should reject duplicate, back-to-back messages from captureMessage when it has stacktrace',
9+
async ({ getLocalTestPath, page }) => {
10+
const url = await getLocalTestPath({ testDir: __dirname });
11+
12+
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 5, { url });
13+
14+
expect(eventData[0].message).toMatch(/Message no \d+/);
15+
expect(eventData[1].message).toMatch(/Message no \d+/);
16+
expect(eventData[2].message).toMatch('same message, same stacktrace');
17+
expect(eventData[3].message).toMatch('same message, different stacktrace');
18+
expect(eventData[4].message).toMatch('same message, different stacktrace');
19+
},
20+
);

packages/browser/test/integration/suites/api.js

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

packages/browser/test/integration/suites/shell.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function runVariant(variant) {
2323
* The test runner will replace each of these placeholders with the contents of the corresponding file.
2424
*/
2525
{{ suites/config.js }} // biome-ignore format: No trailing commas
26-
{{ suites/api.js }} // biome-ignore format: No trailing commas
2726
{{ suites/onerror.js }} // biome-ignore format: No trailing commas
2827
{{ suites/onunhandledrejection.js }} // biome-ignore format: No trailing commas
2928
{{ suites/builtins.js }} // biome-ignore format: No trailing commas

0 commit comments

Comments
 (0)