Skip to content

Migrate browser tests from Karma to Web Test Runner#1300

Merged
matux merged 8 commits into
matux/wtrfrom
matux/wtr-browser
Sep 8, 2025
Merged

Migrate browser tests from Karma to Web Test Runner#1300
matux merged 8 commits into
matux/wtrfrom
matux/wtr-browser

Conversation

@matux

@matux matux commented Sep 8, 2025

Copy link
Copy Markdown
Contributor

Note

  • This PR is being merged into a feature branch: matux/wtr.
  • Multiple comments in the diff with details/reasoning about specific changes.
  • The PR description was mostly generated by Claude.

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

  • ✅ Migrated all remaining browser tests from Karma to Web Test Runner
  • ✅ Converted all test files from CommonJS (require/module.exports) to ES modules (import/export)
  • ✅ Replaced done-callback async patterns with modern async/await
  • ✅ Added web-test-runner.config.mjs configuration
  • ✅ Created test utilities:
    • test/util/timers.js - Promise-based setTimeout helper
    • test/util/fixtures.js - HTML fixture loading utility

Test Organization

  • 📁 Extracted large autoInstrument test section from browser.rollbar.test.js into focused test files:
    • browser.rollbar.autoInstrument.test.js - Core autoInstrument tests
    • browser.rollbar.autoInstrument.xhr.test.js - XHR telemetry tests
    • browser.rollbar.autoInstrument.fetch.test.js - Fetch telemetry tests
    • browser.rollbar.autoInstrument.csp.test.js - Content Security Policy tests
  • 📁 Created browser.rollbar.test-utils.js for shared test utilities and stubs

Key Improvements

  • 🚀 Faster test execution - WTR runs tests significantly faster than Karma
  • 🔧 Better debugging - Native browser DevTools support with --manual flag
  • 📦 Simplified dependencies - Removed Karma and its associated plugins
  • 🎯 Modern patterns - Async/await makes tests more readable and maintainable
  • 🧩 Better test organization - Separated concerns for easier maintenance

Technical Details

CSP Test Fix

Fixed Content Security Policy violation test that was failing under WTR by simulating SecurityPolicyViolationEvent directly. Details: https://github.com/rollbar/rollbar.js/pull/1300/files#r2331149823

ES Module Migration Pattern

// Before (CommonJS)
var expect = require('chai').expect;
module.exports = { TestClientGen };

// After (ES Modules)
import { expect } from 'chai';
export { TestClientGen };

Async Pattern Migration

// Before (done callback)
it('should test async', function(done) {
  setTimeout(function() {
    expect(true).to.be.true;
    done();
  }, 100);
});

// After (async/await)
it('should test async', async function() {
  await setTimeout(100);
  expect(true).to.be.true;
});

Testing

  • ✅ All browser tests passing with Web Test Runner
  • ✅ CI workflow updated and passing
  • ✅ Backward compatibility maintained - no changes to SDK functionality

Next Steps

Future PRs will continue the modernization effort:

  • Further break down large test files for better maintainability
  • Complete migration of any remaining Karma configuration
  • Add TypeScript support to the build pipeline

@matux matux self-assigned this Sep 8, 2025
Comment thread test/fixtures/html/csp-errors.html Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were extracted from browser.rollbar.test.js just for consistency (so all autoInstrument tests live separately from the main browser.rollbar.test.js.)

Comment on lines +34 to +47
// 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,
},
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matux matux requested a review from Copilot September 8, 2025 19:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/browser.rollbar.test.js Outdated
Comment thread test/browser.rollbar.test.js
Comment thread test/browser.rollbar.test.js
Comment thread test/browser.rollbar.test.js
Comment thread test/browser.rollbar.test.js
Comment thread test/browser.rollbar.test.js
@matux matux merged commit 21de105 into matux/wtr Sep 8, 2025
4 checks passed
@matux matux deleted the matux/wtr-browser branch September 8, 2025 20:42
matux added a commit that referenced this pull request Sep 8, 2025
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants