Migrate browser tests from Karma to Web Test Runner#1300
Conversation
There was a problem hiding this comment.
I wasn't able to make this work by loading the HTML, for some reason WTR+Mocha would capture the CSP event itself and bail out.
This is was happening in our new custom loadHtml() function here:
await new Promise((resolve, reject) => {
newScript.onload = resolve;
newScript.onerror = reject; // <-- This rejects the promise on script error
document.body.appendChild(newScript);
});So, the rejected promise cause the test to fail with:
❌ options.autoInstrument > contentSecurityPolicy > should report content security policy errors
Error: the event {"isTrusted":true} was thrown, throw an Error :)
at Ws (node_modules/@web/test-runner-mocha/dist/autorun.js:1:204946)
at Ys.fail (node_modules/@web/test-runner-mocha/dist/autorun.js:1:207579)
at node_modules/@web/test-runner-mocha/dist/autorun.js:1:210970
at s (node_modules/@web/test-runner-mocha/dist/autorun.js:1:194229)
at node_modules/@web/test-runner-mocha/dist/autorun.js:1:195193
I couldn't find a way to dissuade the test runner from meddling. After some research, I found out you can trigger a CSP violation event programmatically, and opted to go that route.
There was a problem hiding this comment.
These tests were extracted from browser.rollbar.test.js since they'd leave a dirty state that would make subsequent tests fail. I suspect this is due to concurrency. Separating these tests into their own file fixed the issues.
There was a problem hiding this comment.
Similar to fetch, these tests were extracted from browser.rollbar.test.js since they'd leave a dirty state that would make subsequent tests fail. I suspect this is due to concurrency. Separating these tests into their own file fixed the issues.
There was a problem hiding this comment.
These were extracted from browser.rollbar.test.js just for consistency (so all autoInstrument tests live separately from the main browser.rollbar.test.js.)
| // Manually trigger a CSP violation event, | ||
| // since WTR+Mocha will capture the rejection from loadHtml. | ||
| const cspEvent = new SecurityPolicyViolationEvent( | ||
| 'securitypolicyviolation', | ||
| { | ||
| blockedURI: 'https://example.com/v3/', | ||
| violatedDirective: 'script-src', | ||
| effectiveDirective: 'script-src', | ||
| originalPolicy: "default-src 'self' 'unsafe-inline' 'unsafe-eval';", | ||
| sourceFile: window.location.href, | ||
| lineNumber: 1, | ||
| columnNumber: 1, | ||
| }, | ||
| ); |
There was a problem hiding this comment.
Refer to this comment for more details: https://github.com/rollbar/rollbar.js/pull/1300/files#r2331149823
There was a problem hiding this comment.
Pull Request Overview
This PR completes the migration of browser tests from the deprecated Karma test runner to the modern Web Test Runner (WTR), modernizing test infrastructure and patterns. The migration includes converting all test files from CommonJS to ES modules and replacing done-callback async patterns with modern async/await.
- Migration from Karma to Web Test Runner for all browser tests
- Conversion from CommonJS to ES modules with modern async/await patterns
- Extraction of large test files into focused, maintainable modules
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/fixtures/html/csp-errors.html | Removed HTML fixture file (no longer needed for WTR approach) |
| test/browser.rollbar.test.js | Migrated main test file to ES modules and async/await patterns |
| test/browser.rollbar.test-utils.js | Created shared utilities module for test stubs and helpers |
| test/browser.rollbar.autoInstrument.xhr.test.js | Extracted XHR telemetry tests into focused module |
| test/browser.rollbar.autoInstrument.test.js | Extracted core autoInstrument tests into focused module |
| test/browser.rollbar.autoInstrument.fetch.test.js | Extracted fetch telemetry tests into focused module |
| test/browser.rollbar.autoInstrument.csp.test.js | Extracted CSP tests into focused module with event simulation |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <[email protected]>
* Initial Karma to Web Test Runner migration (#1289) * Fix hanging tests and expand WTR migration (#1295) * Migrate example tests for Web Test Runner (#1298) * Migrate browser transform and core tests for Web Test Runner (#1299) * Migrate browser tests from Karma to Web Test Runner (#1300) * Initial migration of react native tests (#1301) * Remove Grunt and Karma (#1302)
Note
matux/wtr.Description of the change
This PR completes the migration of browser tests from the deprecated Karma test runner to the modern Web Test Runner (WTR), continuing the modernization effort started in #1289. All browser tests have been converted from CommonJS to ES modules and updated to use modern async/await patterns.
Changes
Test Infrastructure
test/util/timers.js- Promise-based setTimeout helpertest/util/fixtures.js- HTML fixture loading utilityTest Organization
browser.rollbar.autoInstrument.test.js- Core autoInstrument testsbrowser.rollbar.autoInstrument.xhr.test.js- XHR telemetry testsbrowser.rollbar.autoInstrument.fetch.test.js- Fetch telemetry testsbrowser.rollbar.autoInstrument.csp.test.js- Content Security Policy testsbrowser.rollbar.test-utils.jsfor shared test utilities and stubsKey Improvements
--manualflagTechnical Details
CSP Test Fix
Fixed Content Security Policy violation test that was failing under WTR by simulating
SecurityPolicyViolationEventdirectly. Details: https://github.com/rollbar/rollbar.js/pull/1300/files#r2331149823ES Module Migration Pattern
Async Pattern Migration
Testing
Next Steps
Future PRs will continue the modernization effort: