Skip to content

Commit c90764b

Browse files
authored
test(browser): Add integration tests for setContext (#4349)
1 parent 8b53d16 commit c90764b

File tree

8 files changed

+95
-0
lines changed

8 files changed

+95
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Sentry.setContext('context_1', {
2+
foo: 'bar',
3+
baz: {
4+
qux: 'quux',
5+
},
6+
});
7+
8+
Sentry.setContext('context_2', {
9+
1: 'foo',
10+
bar: false,
11+
});
12+
13+
Sentry.setContext('context_3', null);
14+
Sentry.setContext('context_4');
15+
Sentry.setContext('context_5', NaN);
16+
Sentry.setContext('context_6', Math.PI);
17+
18+
Sentry.captureMessage('multiple_contexts');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getSentryRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should record multiple contexts', async ({ getLocalTestPath, page }) => {
7+
const url = await getLocalTestPath({ testDir: __dirname });
8+
9+
const eventData = await getSentryRequest(page, url);
10+
11+
expect(eventData.message).toBe('multiple_contexts');
12+
expect(eventData.contexts).toMatchObject({
13+
context_1: {
14+
foo: 'bar',
15+
baz: { qux: 'quux' },
16+
},
17+
context_2: { 1: 'foo', bar: false },
18+
context_4: '[undefined]',
19+
context_5: '[NaN]',
20+
context_6: 3.141592653589793,
21+
});
22+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const el = document.querySelector('body');
2+
3+
Sentry.setContext('non_serializable', el);
4+
5+
Sentry.captureMessage('non_serializable');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getSentryRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should normalize non-serializable context', async ({ getLocalTestPath, page }) => {
7+
const url = await getLocalTestPath({ testDir: __dirname });
8+
9+
const eventData = await getSentryRequest(page, url);
10+
11+
expect(eventData.contexts?.non_serializable).toMatchObject({});
12+
expect(eventData.message).toBe('non_serializable');
13+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Sentry.setContext('foo', { bar: 'baz' });
2+
Sentry.captureMessage('simple_context_object');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getSentryRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should set a simple context', async ({ getLocalTestPath, page }) => {
7+
const url = await getLocalTestPath({ testDir: __dirname });
8+
9+
const eventData = await getSentryRequest(page, url);
10+
11+
expect(eventData.message).toBe('simple_context_object');
12+
expect(eventData.contexts).toMatchObject({
13+
foo: {
14+
bar: 'baz',
15+
},
16+
});
17+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title></title>
6+
<script src="{{htmlWebpackPlugin.options.initialization}}"></script>
7+
</head>
8+
<body>
9+
<script src="{{htmlWebpackPlugin.options.subject}}"></script>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)