Skip to content

Migrate example tests for Web Test Runner#1298

Merged
matux merged 2 commits into
matux/wtrfrom
matux/wtr-examples
Sep 5, 2025
Merged

Migrate example tests for Web Test Runner#1298
matux merged 2 commits into
matux/wtrfrom
matux/wtr-examples

Conversation

@matux

@matux matux commented Sep 4, 2025

Copy link
Copy Markdown
Contributor

Caution

This PR is being merged into a feature branch: matux/wtr.

Note

Multiple comments in the diff with details/reasoning about specific changes.

Description of the change

This PR completes the migration of example tests from Karma to Web Test Runner, fixing critical issues with loading HTML fixtures and handling uncaught errors.

New Test Utilities

  • test/util/fixtures.js: Added loadHtml helper to load HTML fixtures in a WTR/Playwright-friendly way with proper script handling
    • Handles both inline and external scripts
    • Preserves script type to prevent module/classic script conflicts in WTR
  • test/util/timers.js: Added promise-based setTimeout to avoid cases where both await and done were needed (which is illegal).

Migrated Example Tests

  • test/examples/react.test.js: Migrated React example test with proper uncaught error handling
  • test/examples/universalBrowser.test.js: Migrated universal browser test
  • test/examples/universalBrowserConfig.test.js: Migrated universal browser config test
  • test/examples/webpack.test.js: Migrated webpack example test

Fixes

  • Fixed script loading issues by preserving type attribute to prevent WTR from treating classic scripts as ES modules
  • Handled uncaught error testing by temporarily disabling Mocha's error handler
  • Updated HTML fixtures to work with WTR's static file serving

Test Results

  • All 369 tests passing
  • Example tests now fully integrated with Web Test Runner
  • Removed dependency on Karma's html fixture loading

Technical Notes

The main two challenges were

  1. WTR/Mocha installs a window.onerror handler that fails tests on any uncaught error. This conflicted with tests verifying Rollbar's error catching. Solution: temporarily disable Mocha's handler during these tests, allowing Rollbar to catch errors without WTR interference.
  2. handling the difference between how Karma and WTR load HTML fixtures and handle scripts. WTR defaults to treating dynamically created scripts as ES modules, which broke bundled classic scripts. The solution was to preserve the original type attribute when recreating script elements.

Related issues

SDK-493/replace-karma-with-webtest-runner-for-modern-performant-browser

@matux matux requested a review from Copilot September 4, 2025 14:26
@matux matux self-assigned this Sep 4, 2025

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Comment on lines 3 to -33
<script>
var domainsBlocked = ['localhost'];

function ignoreFilesUncaught(isUncaught, args, payload) {
try {
var filename = payload.data.body.trace.frames[0].filename;

var isBlocked = domainsBlocked.some(function (domain) {
return filename.indexOf(domain) !== -1;
});

if (isUncaught && isBlocked) {
console.log('Ignoring error from ' + filename);
return true;
}
} catch (e) {
console.error(e);
}
return false;
}

var _rollbarConfig = {
let _rollbarConfig = {
accessToken: 'POST_CLIENT_ITEM_TOKEN',
rollbarJsUrl: '/dist/rollbar.js',
captureUncaught: true,
//checkIgnore: ignoreFilesUncaught,
payload: {
environment: 'test',
},
};
</script>

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.

This script wasn't being used.

<title>Webpack Example</title>

<script src="../dist/bundle.js"></script>
<script src="/examples/webpack/dist/bundle.js"></script>

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.

WTR/Playwright quirk, uses project root not current dir

Comment on lines -14 to +19
// Karma headless chrome won't dispatch DOMContentLoaded,
// so we need to do it manually.
var event = document.createEvent('Event');
event.initEvent('DOMContentLoaded', true, true);
document.dispatchEvent(event);
// DOMContentLoaded is not dispatched automatically in WTR/Playwright
document.dispatchEvent(new Event('DOMContentLoaded', { bubbles: true }));

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.

event.initEvent is deprecated.

Comment on lines -21 to +22
setTimeout(function () {
done();
}, 1000);
await setTimeout(250);

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.

1s wait is unnecessary and slows the test down, 10 is enough, left 250 just in case.

Comment on lines +11 to +13
// Prevent WTR/Mocha from failing the test on uncaught errors.
__originalOnError = window.onerror;
window.onerror = () => false;

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.

We store the original error and replace it with one that just returns false to signal the exception has been handled and to prevent propagation. Then Rollbar will swap its onerror with this one instead of WTR/Mocha/React's.

Comment on lines -12 to +16
document.write(window.__html__['examples/universal-browser/test.html']);
await loadHtml('examples/universal-browser/test.html');

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.

write is deprecated. window.__html__ is Karma magic, so we needed a new way to load HTML.

Comment thread .prettierignore
# 1. there being minified snippets all over the place and
# 2. some tests fail cause they depend on some sources being exactly as they are
examples
!test/examples

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.

prettify was ignoring js files in test/examples.

@brianr brianr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@matux matux merged commit c2de6ac into matux/wtr Sep 5, 2025
4 checks passed
@matux matux deleted the matux/wtr-examples branch September 5, 2025 18:45
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