Skip to content

Commit cc0fcb8

Browse files
authored
test(e2e): Add Vue 3 E2E tests (#10476)
This PR adds e2e tests for a Vue 3 app using `@sentry/vue` Specifically, we test - Catching an error - Pageload transaction - Navigation transaction - Preferring route name over route id
1 parent 3b2b18c commit cc0fcb8

28 files changed

+905
-3
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,8 @@ jobs:
10311031
'node-experimental-fastify-app',
10321032
'node-hapi-app',
10331033
'node-exports-test-app',
1034-
'node-profiling'
1034+
'node-profiling',
1035+
'vue-3'
10351036
]
10361037
build-command:
10371038
- false

biome.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
".next/**",
4242
".svelte-kit/**",
4343
".angular/**",
44-
"angular.json"
44+
"angular.json",
45+
"ember/instance-initializers/**",
46+
"ember/types.d.ts"
4547
]
4648
},
4749
"files": {
@@ -65,7 +67,9 @@
6567
".svelte-kit/**",
6668
".angular/**",
6769
"angular.json",
68-
"**/profiling-node/lib/**"
70+
"**/profiling-node/lib/**",
71+
"ember/instance-initializers/**",
72+
"ember/types.d.ts"
6973
]
7074
},
7175
"javascript": {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
.DS_Store
12+
dist
13+
dist-ssr
14+
coverage
15+
*.local
16+
17+
/cypress/videos/
18+
/cypress/screenshots/
19+
20+
# Editor directories and files
21+
.vscode/*
22+
!.vscode/extensions.json
23+
.idea
24+
*.suo
25+
*.ntvs*
26+
*.njsproj
27+
*.sln
28+
*.sw?
29+
30+
*.tsbuildinfo
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Vue 3 E2E Test App
2+
3+
E2E test app for Vue 3 and `@sentry/vue`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
import * as fs from 'fs';
2+
import * as http from 'http';
3+
import * as https from 'https';
4+
import type { AddressInfo } from 'net';
5+
import * as os from 'os';
6+
import * as path from 'path';
7+
import * as util from 'util';
8+
import * as zlib from 'zlib';
9+
import type { Envelope, EnvelopeItem, SerializedEvent } from '@sentry/types';
10+
import { parseEnvelope } from '@sentry/utils';
11+
12+
const readFile = util.promisify(fs.readFile);
13+
const writeFile = util.promisify(fs.writeFile);
14+
15+
interface EventProxyServerOptions {
16+
/** Port to start the event proxy server at. */
17+
port: number;
18+
/** The name for the proxy server used for referencing it with listener functions */
19+
proxyServerName: string;
20+
}
21+
22+
interface SentryRequestCallbackData {
23+
envelope: Envelope;
24+
rawProxyRequestBody: string;
25+
rawSentryResponseBody: string;
26+
sentryResponseStatusCode?: number;
27+
}
28+
29+
/**
30+
* Starts an event proxy server that will proxy events to sentry when the `tunnel` option is used. Point the `tunnel`
31+
* option to this server (like this `tunnel: http://localhost:${port option}/`).
32+
*/
33+
export async function startEventProxyServer(options: EventProxyServerOptions): Promise<void> {
34+
const eventCallbackListeners: Set<(data: string) => void> = new Set();
35+
36+
const proxyServer = http.createServer((proxyRequest, proxyResponse) => {
37+
const proxyRequestChunks: Uint8Array[] = [];
38+
39+
proxyRequest.addListener('data', (chunk: Buffer) => {
40+
proxyRequestChunks.push(chunk);
41+
});
42+
43+
proxyRequest.addListener('error', err => {
44+
throw err;
45+
});
46+
47+
proxyRequest.addListener('end', () => {
48+
const proxyRequestBody =
49+
proxyRequest.headers['content-encoding'] === 'gzip'
50+
? zlib.gunzipSync(Buffer.concat(proxyRequestChunks)).toString()
51+
: Buffer.concat(proxyRequestChunks).toString();
52+
53+
let envelopeHeader = JSON.parse(proxyRequestBody.split('\n')[0]);
54+
55+
if (!envelopeHeader.dsn) {
56+
throw new Error('[event-proxy-server] No dsn on envelope header. Please set tunnel option.');
57+
}
58+
59+
const { origin, pathname, host } = new URL(envelopeHeader.dsn);
60+
61+
const projectId = pathname.substring(1);
62+
const sentryIngestUrl = `${origin}/api/${projectId}/envelope/`;
63+
64+
proxyRequest.headers.host = host;
65+
66+
const sentryResponseChunks: Uint8Array[] = [];
67+
68+
const sentryRequest = https.request(
69+
sentryIngestUrl,
70+
{ headers: proxyRequest.headers, method: proxyRequest.method },
71+
sentryResponse => {
72+
sentryResponse.addListener('data', (chunk: Buffer) => {
73+
proxyResponse.write(chunk, 'binary');
74+
sentryResponseChunks.push(chunk);
75+
});
76+
77+
sentryResponse.addListener('end', () => {
78+
eventCallbackListeners.forEach(listener => {
79+
const rawSentryResponseBody = Buffer.concat(sentryResponseChunks).toString();
80+
81+
const data: SentryRequestCallbackData = {
82+
envelope: parseEnvelope(proxyRequestBody, new TextEncoder(), new TextDecoder()),
83+
rawProxyRequestBody: proxyRequestBody,
84+
rawSentryResponseBody,
85+
sentryResponseStatusCode: sentryResponse.statusCode,
86+
};
87+
88+
listener(Buffer.from(JSON.stringify(data)).toString('base64'));
89+
});
90+
proxyResponse.end();
91+
});
92+
93+
sentryResponse.addListener('error', err => {
94+
throw err;
95+
});
96+
97+
proxyResponse.writeHead(sentryResponse.statusCode || 500, sentryResponse.headers);
98+
},
99+
);
100+
101+
sentryRequest.write(Buffer.concat(proxyRequestChunks), 'binary');
102+
sentryRequest.end();
103+
});
104+
});
105+
106+
const proxyServerStartupPromise = new Promise<void>(resolve => {
107+
proxyServer.listen(options.port, () => {
108+
resolve();
109+
});
110+
});
111+
112+
const eventCallbackServer = http.createServer((eventCallbackRequest, eventCallbackResponse) => {
113+
eventCallbackResponse.statusCode = 200;
114+
eventCallbackResponse.setHeader('connection', 'keep-alive');
115+
116+
const callbackListener = (data: string): void => {
117+
eventCallbackResponse.write(data.concat('\n'), 'utf8');
118+
};
119+
120+
eventCallbackListeners.add(callbackListener);
121+
122+
eventCallbackRequest.on('close', () => {
123+
eventCallbackListeners.delete(callbackListener);
124+
});
125+
126+
eventCallbackRequest.on('error', () => {
127+
eventCallbackListeners.delete(callbackListener);
128+
});
129+
});
130+
131+
const eventCallbackServerStartupPromise = new Promise<void>(resolve => {
132+
eventCallbackServer.listen(0, () => {
133+
const port = String((eventCallbackServer.address() as AddressInfo).port);
134+
void registerCallbackServerPort(options.proxyServerName, port).then(resolve);
135+
});
136+
});
137+
138+
await eventCallbackServerStartupPromise;
139+
await proxyServerStartupPromise;
140+
return;
141+
}
142+
143+
export async function waitForRequest(
144+
proxyServerName: string,
145+
callback: (eventData: SentryRequestCallbackData) => Promise<boolean> | boolean,
146+
): Promise<SentryRequestCallbackData> {
147+
const eventCallbackServerPort = await retrieveCallbackServerPort(proxyServerName);
148+
149+
return new Promise<SentryRequestCallbackData>((resolve, reject) => {
150+
const request = http.request(`http://localhost:${eventCallbackServerPort}/`, {}, response => {
151+
let eventContents = '';
152+
153+
response.on('error', err => {
154+
reject(err);
155+
});
156+
157+
response.on('data', (chunk: Buffer) => {
158+
const chunkString = chunk.toString('utf8');
159+
chunkString.split('').forEach(char => {
160+
if (char === '\n') {
161+
const eventCallbackData: SentryRequestCallbackData = JSON.parse(
162+
Buffer.from(eventContents, 'base64').toString('utf8'),
163+
);
164+
const callbackResult = callback(eventCallbackData);
165+
if (typeof callbackResult !== 'boolean') {
166+
callbackResult.then(
167+
match => {
168+
if (match) {
169+
response.destroy();
170+
resolve(eventCallbackData);
171+
}
172+
},
173+
err => {
174+
throw err;
175+
},
176+
);
177+
} else if (callbackResult) {
178+
response.destroy();
179+
resolve(eventCallbackData);
180+
}
181+
eventContents = '';
182+
} else {
183+
eventContents = eventContents.concat(char);
184+
}
185+
});
186+
});
187+
});
188+
189+
request.end();
190+
});
191+
}
192+
193+
export function waitForEnvelopeItem(
194+
proxyServerName: string,
195+
callback: (envelopeItem: EnvelopeItem) => Promise<boolean> | boolean,
196+
): Promise<EnvelopeItem> {
197+
return new Promise((resolve, reject) => {
198+
waitForRequest(proxyServerName, async eventData => {
199+
const envelopeItems = eventData.envelope[1];
200+
for (const envelopeItem of envelopeItems) {
201+
if (await callback(envelopeItem)) {
202+
resolve(envelopeItem);
203+
return true;
204+
}
205+
}
206+
return false;
207+
}).catch(reject);
208+
});
209+
}
210+
211+
export function waitForError(
212+
proxyServerName: string,
213+
callback: (transactionEvent: SerializedEvent) => Promise<boolean> | boolean,
214+
): Promise<SerializedEvent> {
215+
return new Promise((resolve, reject) => {
216+
waitForEnvelopeItem(proxyServerName, async envelopeItem => {
217+
const [envelopeItemHeader, envelopeItemBody] = envelopeItem;
218+
if (envelopeItemHeader.type === 'event' && (await callback(envelopeItemBody as SerializedEvent))) {
219+
resolve(envelopeItemBody as SerializedEvent);
220+
return true;
221+
}
222+
return false;
223+
}).catch(reject);
224+
});
225+
}
226+
227+
export function waitForTransaction(
228+
proxyServerName: string,
229+
callback: (transactionEvent: SerializedEvent) => Promise<boolean> | boolean,
230+
): Promise<SerializedEvent> {
231+
return new Promise((resolve, reject) => {
232+
waitForEnvelopeItem(proxyServerName, async envelopeItem => {
233+
const [envelopeItemHeader, envelopeItemBody] = envelopeItem;
234+
if (envelopeItemHeader.type === 'transaction' && (await callback(envelopeItemBody as SerializedEvent))) {
235+
resolve(envelopeItemBody as SerializedEvent);
236+
return true;
237+
}
238+
return false;
239+
}).catch(reject);
240+
});
241+
}
242+
243+
const TEMP_FILE_PREFIX = 'event-proxy-server-';
244+
245+
async function registerCallbackServerPort(serverName: string, port: string): Promise<void> {
246+
const tmpFilePath = path.join(os.tmpdir(), `${TEMP_FILE_PREFIX}${serverName}`);
247+
await writeFile(tmpFilePath, port, { encoding: 'utf8' });
248+
}
249+
250+
function retrieveCallbackServerPort(serverName: string): Promise<string> {
251+
const tmpFilePath = path.join(os.tmpdir(), `${TEMP_FILE_PREFIX}${serverName}`);
252+
return readFile(tmpFilePath, 'utf8');
253+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<link rel="icon" href="/favicon.ico">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Vite App</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "vue-3-tmp",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"clean": "npx rimraf node_modules,pnpm-lock.yaml,dist",
8+
"dev": "vite",
9+
"build": "run-p type-check \"build-only {@}\" --",
10+
"preview": "vite preview",
11+
"build-only": "vite build",
12+
"type-check": "vue-tsc --build --force",
13+
"test": "playwright test",
14+
"test:build": "pnpm install && npx playwright install && pnpm build",
15+
"test:assert": "playwright test"
16+
},
17+
"dependencies": {
18+
"@sentry/vue": "latest || *",
19+
"vue": "^3.4.15",
20+
"vue-router": "^4.2.5"
21+
},
22+
"devDependencies": {
23+
"@playwright/test": "^1.41.1",
24+
"@sentry/types": "^7.99.0",
25+
"@sentry/utils": "^7.99.0",
26+
"@tsconfig/node20": "^20.1.2",
27+
"@types/node": "^20.11.10",
28+
"@vitejs/plugin-vue": "^5.0.3",
29+
"@vitejs/plugin-vue-jsx": "^3.1.0",
30+
"@vue/tsconfig": "^0.5.1",
31+
"http-server": "^14.1.1",
32+
"npm-run-all2": "^6.1.1",
33+
"ts-node": "10.9.1",
34+
"typescript": "~5.3.0",
35+
"vite": "^5.0.11",
36+
"vue-tsc": "^1.8.27",
37+
"wait-port": "1.0.4"
38+
},
39+
"volta": {
40+
"extends": "../../package.json"
41+
}
42+
}

0 commit comments

Comments
 (0)