Skip to content

Commit e057674

Browse files
authored
test(browser): Test webpack 5 + Terser (#11249)
Tests that an app built with webpack 5 and minified with terser can be built and sends events at runtime
1 parent 693f6ca commit e057674

File tree

7 files changed

+191
-1
lines changed

7 files changed

+191
-1
lines changed

.github/workflows/build.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,8 @@ jobs:
10481048
# TODO(v8): Re-enable hapi tests
10491049
# 'node-hapi-app',
10501050
'node-exports-test-app',
1051-
'vue-3'
1051+
'vue-3',
1052+
'webpack-5'
10521053
]
10531054
build-command:
10541055
- false
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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, 'entry.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: [new HtmlWebpackPlugin(), new webpack.EnvironmentPlugin(['E2E_TEST_DSN'])],
21+
mode: 'production',
22+
},
23+
(err, stats) => {
24+
if (err) {
25+
console.error(err.stack || err);
26+
if (err.details) {
27+
console.error(err.details);
28+
}
29+
return;
30+
}
31+
32+
const info = stats.toJson();
33+
34+
if (stats.hasErrors()) {
35+
console.error(info.errors);
36+
process.exit(1);
37+
}
38+
39+
if (stats.hasWarnings()) {
40+
console.warn(info.warnings);
41+
process.exit(1);
42+
}
43+
},
44+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { browserTracingIntegration, captureException, init } from '@sentry/browser';
2+
3+
init({
4+
dsn: process.env.E2E_TEST_DSN,
5+
integrations: [browserTracingIntegration()],
6+
});
7+
8+
setTimeout(() => {
9+
const eventId = captureException(new Error('I am an error!'));
10+
window.capturedExceptionId = eventId;
11+
}, 2000);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "webpack-5-test",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"start": "serve -s build",
6+
"build": "node build.mjs",
7+
"test:build": "pnpm install && npx playwright install && pnpm build",
8+
"test:assert": "playwright test"
9+
},
10+
"devDependencies": {
11+
"@playwright/test": "^1.42.1",
12+
"@sentry/browser": "latest || *",
13+
"webpack": "^5.91.0",
14+
"terser-webpack-plugin": "^5.3.10",
15+
"html-webpack-plugin": "^5.6.0",
16+
"serve": "^14.2.1"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { PlaywrightTestConfig } from '@playwright/test';
2+
import { devices } from '@playwright/test';
3+
4+
/**
5+
* See https://playwright.dev/docs/test-configuration.
6+
*/
7+
const config: PlaywrightTestConfig = {
8+
testDir: './tests',
9+
/* Maximum time one test can run for. */
10+
timeout: 150_000,
11+
expect: {
12+
/**
13+
* Maximum time expect() should wait for the condition to be met.
14+
* For example in `await expect(locator).toHaveText();`
15+
*/
16+
timeout: 5000,
17+
},
18+
/* Run tests in files in parallel */
19+
fullyParallel: true,
20+
/* Fail the build on CI if you accidentally left test.only in the source code. */
21+
forbidOnly: !!process.env.CI,
22+
/* Retry on CI only */
23+
retries: 0,
24+
/* Opt out of parallel tests on CI. */
25+
workers: 1,
26+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
27+
reporter: 'list',
28+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
29+
use: {
30+
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
31+
actionTimeout: 0,
32+
33+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
34+
trace: 'on-first-retry',
35+
},
36+
37+
/* Configure projects for major browsers */
38+
projects: [
39+
{
40+
name: 'chromium',
41+
use: {
42+
...devices['Desktop Chrome'],
43+
},
44+
},
45+
// For now we only test Chrome!
46+
// {
47+
// name: 'firefox',
48+
// use: {
49+
// ...devices['Desktop Firefox'],
50+
// },
51+
// },
52+
// {
53+
// name: 'webkit',
54+
// use: {
55+
// ...devices['Desktop Safari'],
56+
// },
57+
// },
58+
],
59+
60+
/* Run your local dev server before starting the tests */
61+
webServer: {
62+
command: 'pnpm start',
63+
port: 3030,
64+
env: {
65+
PORT: '3030',
66+
},
67+
},
68+
};
69+
70+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expect, test } from '@playwright/test';
2+
import axios, { AxiosError } from 'axios';
3+
4+
const EVENT_POLLING_TIMEOUT = 90_000;
5+
6+
const authToken = process.env.E2E_TEST_AUTH_TOKEN;
7+
const sentryTestOrgSlug = process.env.E2E_TEST_SENTRY_ORG_SLUG;
8+
const sentryTestProject = process.env.E2E_TEST_SENTRY_TEST_PROJECT;
9+
10+
test('Sends an exception to Sentry', async ({ page }) => {
11+
await page.goto('/');
12+
13+
const exceptionIdHandle = await page.waitForFunction(() => window.capturedExceptionId);
14+
const exceptionEventId = await exceptionIdHandle.jsonValue();
15+
16+
console.log(`Polling for error eventId: ${exceptionEventId}`);
17+
18+
await expect
19+
.poll(
20+
async () => {
21+
try {
22+
const response = await axios.get(
23+
`https://sentry.io/api/0/projects/${sentryTestOrgSlug}/${sentryTestProject}/events/${exceptionEventId}/`,
24+
{ headers: { Authorization: `Bearer ${authToken}` } },
25+
);
26+
return response.status;
27+
} catch (e) {
28+
if (e instanceof AxiosError && e.response) {
29+
if (e.response.status !== 404) {
30+
throw e;
31+
} else {
32+
return e.response.status;
33+
}
34+
} else {
35+
throw e;
36+
}
37+
}
38+
},
39+
{
40+
timeout: EVENT_POLLING_TIMEOUT,
41+
},
42+
)
43+
.toBe(200);
44+
});

0 commit comments

Comments
 (0)