-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
71 lines (52 loc) · 2.31 KB
/
test.ts
File metadata and controls
71 lines (52 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { expect } from '@playwright/test';
import * as fs from 'fs';
import * as path from 'path';
import { sentryTest, TEST_HOST } from '../../../../utils/fixtures';
import { LOADER_CONFIGS } from '../../../../utils/generatePlugin';
import { envelopeRequestParser, waitForErrorRequestOnUrl } from '../../../../utils/helpers';
const bundle = process.env.PW_BUNDLE || '';
const isLazy = LOADER_CONFIGS[bundle]?.lazy;
sentryTest('it does not download the SDK if the SDK was loaded in the meanwhile', async ({ getLocalTestUrl, page }) => {
// When the loader is eager, this does not work and makes no sense
if (isLazy !== true) {
sentryTest.skip();
}
let cdnLoadedCount = 0;
let sentryEventCount = 0;
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
sentryEventCount++;
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});
const tmpDir = await getLocalTestUrl({ testDir: __dirname, skipRouteHandler: true, skipDsnRouteHandler: true });
await page.route(`${TEST_HOST}/*.*`, route => {
const file = route.request().url().split('/').pop();
if (file === 'cdn.bundle.js') {
cdnLoadedCount++;
}
const filePath = path.resolve(tmpDir, `./${file}`);
return fs.existsSync(filePath) ? route.fulfill({ path: filePath }) : route.continue();
});
const url = `${TEST_HOST}/index.html`;
const req = await waitForErrorRequestOnUrl(page, url);
const eventData = envelopeRequestParser(req);
await waitForFunction(() => cdnLoadedCount === 2);
// Still loaded the CDN bundle twice
expect(cdnLoadedCount).toBe(2);
// But only sent to Sentry once
expect(sentryEventCount).toBe(1);
// Ensure loader does not overwrite init/config
const options = await page.evaluate(() => (window as any).Sentry.getClient()?.getOptions());
expect(options?.replaysSessionSampleRate).toBe(0.42);
expect(eventData.exception?.values?.length).toBe(1);
expect(eventData.exception?.values?.[0]?.value).toBe('window.doSomethingWrong is not a function');
});
async function waitForFunction(cb: () => boolean, timeout = 2000, increment = 100) {
while (timeout > 0 && !cb()) {
await new Promise(resolve => setTimeout(resolve, increment));
await waitForFunction(cb, timeout - increment, increment);
}
}