Skip to content

Commit

Permalink
Include output from QUnit.on('error')
Browse files Browse the repository at this point in the history
The two are not likely to overlap, but this could in theory report
the same error twice. In practice, given the bridge kicks in fairly
late, uncaught errors will happen before our QUnit listener is
active, and late errors are likely exclusive to `QUnit.on('error')`.

The "No tests" message is changing in QUnit 3.0 from a "fake" test,
to an error event, hence this is in preparation for that.
  • Loading branch information
Krinkle committed Jul 19, 2024
1 parent 8d84616 commit 6fc27b8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ module.exports = function(grunt) {
>> Expected: true`)) {
cb(err !== null);

// qunit:failNoTests
} else if (/test\/qunit_fail_notests\.html/.test(stdout) &&
stdout.includes(`>> global failure
>> Message: No tests were run.`)) {
cb(err !== null);

// qunit:failPageTimeout
} else if (/test\/qunit_page_timeout\.html/.test(stdout) &&
/Chrome timed out/.test(stdout)) {
Expand Down Expand Up @@ -134,6 +140,9 @@ module.exports = function(grunt) {
failAssert: {
command: 'grunt qunit:failAssert --with-failing'
},
failNoTests: {
command: 'grunt qunit:failNoTests --with-failing'
},
failCircularObject: {
command: 'grunt qunit:failCircularObject --with-failing'
},
Expand All @@ -156,6 +165,13 @@ module.exports = function(grunt) {
]
}
});
grunt.config.set('qunit.failNoTests', {
options: {
urls: [
'http://localhost:9000/test/qunit_fail_notests.html'
]
}
});
grunt.config.set('qunit.failCircularObject', {
options: {
urls: [
Expand Down
4 changes: 4 additions & 0 deletions chrome/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
// QUnit reporter events
// https://qunitjs.com/api/callbacks/QUnit.on/

QUnit.on('error', function(error) {
sendMessage('qunit.on.error', error);
});

QUnit.on('testStart', function(obj) {
sendMessage('qunit.on.testStart', obj);
});
Expand Down
21 changes: 19 additions & 2 deletions tasks/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,30 @@ module.exports = function(grunt) {
combinedRunEnd.status = 'failed';
});

eventBus.on('error.onError', function (msg) {
eventBus.on('qunit.on.error', function (err) {
// It is the responsibility of QUnit to ensure a run is marked as failure
// if there are (unexpected) messages received from window.onerror.
//
// Prior to QUnit 2.17, details of global failures were printed by
// creating a fake test with "testEnd" event. Now, it is our responsiblity
// to print these, via browser-level pageerror or `QUnit.on('error')`.
// to print via `QUnit.on('error')`.
//
// NOTE: Avoid relying solely on Puppeteer's "pageerror" event, as that only
// catches the subset of execution errors that make their way to window.onerror.
// It misses out on unhandled rejections from the browser, the "No tests were run"
// internal QUnit error, and anything else reported to QUnit.onUncaughtException
// by QUnit plugins.
// https://qunitjs.com/api/extension/QUnit.onUncaughtException/
grunt.log.writeln();
grunt.log.error(err.stack || err);
grunt.event.emit('qunit.error.onError', err);
});

eventBus.on('error.onError', function (msg) {
// This is important in addition to `QUnit.on('error')` to catch uncaught
// errors that happen before the bridge is in effect (which in practice
// will happen at DOMContentLoaded, after qunit.js and test files have done
// their initial execution, but before QUnit.begin event and any actual tests).
grunt.log.writeln();
grunt.log.error(msg.stack || msg);
grunt.event.emit('qunit.error.onError', msg);
Expand Down
13 changes: 13 additions & 0 deletions test/qunit_fail_notests.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test Suite</title>
<link rel="stylesheet" href="../node_modules/qunit/qunit/qunit.css" media="screen">
<script src="../node_modules/qunit/qunit/qunit.js"></script>
</head>
<body>
<div id="qunit"></div>
<!-- No tests -->
</body>
</html>

0 comments on commit 6fc27b8

Please sign in to comment.