Skip to content

Commit 6aeaf42

Browse files
Zen-cronicLms24
andauthored
test(browser): Add e2e tests for the @sentry/browser package (#13125)
Add e2e test app for `@sentry/browser` package, testing - error - pageload span - navigation span --------- Co-authored-by: Lukas Stracke <[email protected]>
1 parent abc7259 commit 6aeaf42

File tree

12 files changed

+372
-0
lines changed

12 files changed

+372
-0
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ jobs:
867867
'create-remix-app-express',
868868
'create-remix-app-express-legacy',
869869
'create-remix-app-express-vite-dev',
870+
'default-browser',
870871
'node-express-esm-loader',
871872
'node-express-esm-preload',
872873
'node-express-esm-without-loader',
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
25+
/test-results/
26+
/playwright-report/
27+
/playwright/.cache/
28+
29+
!*.d.ts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@sentry:registry=http://127.0.0.1:4873
2+
@sentry-internal:registry=http://127.0.0.1:4873
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as path from 'path';
2+
import * as url from 'url';
3+
import HtmlWebpackPlugin from 'html-webpack-plugin';
4+
import TerserPlugin from 'terser-webpack-plugin';
5+
import webpack from 'webpack';
6+
7+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
8+
9+
webpack(
10+
{
11+
entry: path.join(__dirname, 'src/index.js'),
12+
output: {
13+
path: path.join(__dirname, 'build'),
14+
filename: 'app.js',
15+
},
16+
optimization: {
17+
minimize: true,
18+
minimizer: [new TerserPlugin()],
19+
},
20+
plugins: [
21+
new webpack.EnvironmentPlugin(['E2E_TEST_DSN']),
22+
new HtmlWebpackPlugin({
23+
template: path.join(__dirname, 'public/index.html'),
24+
}),
25+
],
26+
mode: 'production',
27+
},
28+
(err, stats) => {
29+
if (err) {
30+
console.error(err.stack || err);
31+
if (err.details) {
32+
console.error(err.details);
33+
}
34+
return;
35+
}
36+
37+
const info = stats.toJson();
38+
39+
if (stats.hasErrors()) {
40+
console.error(info.errors);
41+
process.exit(1);
42+
}
43+
44+
if (stats.hasWarnings()) {
45+
console.warn(info.warnings);
46+
process.exit(1);
47+
}
48+
},
49+
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "default-browser-test-app",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@sentry/browser": "latest || *",
7+
"@types/node": "16.7.13",
8+
"typescript": "4.9.5"
9+
},
10+
"scripts": {
11+
"start": "serve -s build",
12+
"build": "node build.mjs",
13+
"test": "playwright test",
14+
"clean": "npx rimraf node_modules pnpm-lock.yaml",
15+
"test:build": "pnpm install && npx playwright install && pnpm build",
16+
"test:assert": "pnpm test"
17+
},
18+
"browserslist": {
19+
"production": [
20+
">0.2%",
21+
"not dead",
22+
"not op_mini all"
23+
],
24+
"development": [
25+
"last 1 chrome version",
26+
"last 1 firefox version",
27+
"last 1 safari version"
28+
]
29+
},
30+
"devDependencies": {
31+
"@playwright/test": "^1.44.1",
32+
"@sentry-internal/test-utils": "link:../../../test-utils",
33+
"webpack": "^5.91.0",
34+
"serve": "14.0.1",
35+
"terser-webpack-plugin": "^5.3.10",
36+
"html-webpack-plugin": "^5.6.0"
37+
},
38+
"volta": {
39+
"extends": "../../package.json"
40+
}
41+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getPlaywrightConfig } from '@sentry-internal/test-utils';
2+
3+
const config = getPlaywrightConfig({
4+
startCommand: `pnpm start`,
5+
});
6+
7+
export default config;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Default Browser App</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
11+
<input type="button" value="Capture Exception" id="exception-button" />
12+
13+
<div id="navigation">
14+
15+
<a id="navigation-link" href="#navigation-target">Navigation Link</a>
16+
17+
<div id="navigation-target">Navigated Element</div>
18+
19+
</div>
20+
21+
<!-- The script tags for the bundled JavaScript files will be injected here by HtmlWebpackPlugin in build.mjs-->
22+
</body>
23+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
Sentry.init({
4+
dsn: process.env.E2E_TEST_DSN,
5+
integrations: [Sentry.browserTracingIntegration()],
6+
tracesSampleRate: 1.0,
7+
release: 'e2e-test',
8+
environment: 'qa',
9+
tunnel: 'http://localhost:3031',
10+
});
11+
12+
document.getElementById('exception-button').addEventListener('click', () => {
13+
throw new Error('I am an error!');
14+
});
15+
16+
document.getElementById('navigation-link').addEventListener('click', () => {
17+
document.getElementById('navigation-target').scrollIntoView({ behavior: 'smooth' });
18+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { startEventProxyServer } from '@sentry-internal/test-utils';
2+
3+
startEventProxyServer({
4+
port: 3031,
5+
proxyServerName: 'default-browser',
6+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';
3+
4+
test('captures an error', async ({ page }) => {
5+
const errorEventPromise = waitForError('default-browser', event => {
6+
return !event.type && event.exception?.values?.[0]?.value === 'I am an error!';
7+
});
8+
9+
await page.goto('/');
10+
11+
const exceptionButton = page.locator('id=exception-button');
12+
await exceptionButton.click();
13+
14+
const errorEvent = await errorEventPromise;
15+
16+
expect(errorEvent.exception?.values).toHaveLength(1);
17+
expect(errorEvent.exception?.values?.[0]?.value).toBe('I am an error!');
18+
19+
expect(errorEvent.transaction).toBe('/');
20+
21+
expect(errorEvent.request).toEqual({
22+
url: 'http://localhost:3030/',
23+
headers: expect.any(Object),
24+
});
25+
26+
expect(errorEvent.contexts?.trace).toEqual({
27+
trace_id: expect.any(String),
28+
span_id: expect.any(String),
29+
});
30+
});
31+
32+
test('sets correct transactionName', async ({ page }) => {
33+
const transactionPromise = waitForTransaction('default-browser', async transactionEvent => {
34+
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
35+
});
36+
37+
const errorEventPromise = waitForError('default-browser', event => {
38+
return !event.type && event.exception?.values?.[0]?.value === 'I am an error!';
39+
});
40+
41+
await page.goto('/');
42+
const transactionEvent = await transactionPromise;
43+
44+
const exceptionButton = page.locator('id=exception-button');
45+
await exceptionButton.click();
46+
47+
const errorEvent = await errorEventPromise;
48+
49+
expect(errorEvent.exception?.values).toHaveLength(1);
50+
expect(errorEvent.exception?.values?.[0]?.value).toBe('I am an error!');
51+
52+
expect(errorEvent.transaction).toEqual('/');
53+
54+
expect(errorEvent.contexts?.trace).toEqual({
55+
trace_id: transactionEvent.contexts?.trace?.trace_id,
56+
span_id: expect.not.stringContaining(transactionEvent.contexts?.trace?.span_id || ''),
57+
});
58+
});

0 commit comments

Comments
 (0)