Skip to content

Commit

Permalink
Remove most QUnit callback forwarding in favour of QUnit events
Browse files Browse the repository at this point in the history
Especially, remove the deprecated details parameter to `QUnit.done()`
as this will be undefined in QUnit 3.0 and thus would cause
grunt-contrib-qunit to crash due to reading `obj.failed` on undefined.

https://qunitjs.com/api/callbacks/QUnit.done/

Most of these are obsoleted by QUnit.on, which are already forwarded
to Grunt's event system as `'qunit.on.*'` and can be used instead.

https://qunitjs.com/api/callbacks/QUnit.on/

To help in the common case, and to ease migration/debugging, leave
forwarding of begin/done events in place but without parameters.
  • Loading branch information
Krinkle committed Jun 9, 2024
1 parent 9b527ee commit 23c08c9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 66 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,14 @@ grunt.initConfig({
When using AMD to load QUnit and your tests, make sure to have a path for the `qunit` module defined.

#### Events and reporting
[QUnit callback](http://api.qunitjs.com/category/callbacks/) methods and arguments are also emitted through grunt's event system so that you may build custom reporting tools. Please refer to to the QUnit documentation for more information.

The events, with arguments, are as follows:
QUnit events are forwarded to Grunt's event system, enabling you to build custom reporting tools. Please refer to the QUnit API documentation on [QUnit events](https://qunitjs.com/api/callbacks/QUnit.on/) and [QUnit callbacks](https://qunitjs.com/api/callbacks/) for when and what data is exposed from these events.

* `qunit.on.testStart` `(obj)`
* `qunit.on.testEnd` `(obj)`
* `qunit.on.runEnd` `(obj)`

* `qunit.begin`
* `qunit.moduleStart` `(name)`
* `qunit.testStart` `(name)`
* `qunit.log` `(result, actual, expected, message, source)`
* `qunit.testDone` `(name, failed, passed, total, duration)`
* `qunit.moduleDone` `(name, failed, passed, total)`
* `qunit.done` `(failed, passed, total, runtime)`
* `qunit.done`

In addition to forwarding QUnit's events, the following events are also emitted by the Grunt plugin:

Expand All @@ -268,6 +261,14 @@ You may listen for these events like so:
grunt.event.on('qunit.spawn', function (url) {
grunt.log.ok('Running test: ' + url);
});
grunt.event.on('qunit.on.testEnd', function (test) {
var name = test.fullName.join(' > ');
if (test.status === 'failed') {
grunt.log.error(name);
} else {
grunt.log.ok(name + ' # ' + test.status);
}
});
```


Expand Down
52 changes: 6 additions & 46 deletions chrome/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}

// QUnit reporter events
// https://api.qunitjs.com/callbacks/QUnit.on/
// https://qunitjs.com/api/callbacks/QUnit.on/

QUnit.on('testStart', function(obj) {
sendMessage('qunit.on.testStart', obj);
Expand All @@ -52,7 +52,8 @@
QUnit.on('testEnd', function(obj) {
// Re-create object to strip out 'assertions' field

// expected and actual may contain circular objects, which would fail in puppeteer as it uses JSON.stringify to serialize its messages
// expected and actual may contain circular objects,
// which would fail in puppeteer as it uses JSON.stringify to serialize its messages
// In that case, replace actual and expected
var errors = obj.errors;
if (!canBeJSONStringified(errors)) {
Expand Down Expand Up @@ -99,55 +100,14 @@
});
});

// QUnit plugin callbacks (for back-compat)
// QUnit plugin callbacks
// https://api.qunitjs.com/callbacks/
//
// TODO: Remove the below in a future major version of grunt-contrib-qunit,
// after updating docs for grunt.event.on() and announcing their deprecation,
// to give developers time to migrate any event consumers to their
// newer equivalents.

QUnit.log(function(obj) {
// What is this I don’t even
if (obj.message === '[object Object], undefined:undefined') {
return;
}

// Parse some stuff before sending it.
var actual;
var expected;

if (!obj.result) {
// Dumping large objects can be very slow, and the dump isn't used for
// passing tests, so only dump if the test failed.
actual = QUnit.dump.parse(obj.actual);
expected = QUnit.dump.parse(obj.expected);
}
// Send it.
sendMessage('qunit.log', obj.result, actual, expected, obj.message, obj.source, obj.todo);
});

QUnit.testStart(function(obj) {
sendMessage('qunit.testStart', obj.name);
});

QUnit.testDone(function(obj) {
sendMessage('qunit.testDone', obj.name, obj.failed, obj.passed, obj.total, obj.runtime, obj.skipped, obj.todo);
});

QUnit.moduleStart(function(obj) {
sendMessage('qunit.moduleStart', obj.name);
});

QUnit.moduleDone(function(obj) {
sendMessage('qunit.moduleDone', obj.name, obj.failed, obj.passed, obj.total);
});

QUnit.begin(function() {
sendMessage('qunit.begin');
});

QUnit.done(function(obj) {
sendMessage('qunit.done', obj.failed, obj.passed, obj.total, obj.runtime);
QUnit.done(function() {
sendMessage('qunit.done');
});
}));
19 changes: 10 additions & 9 deletions docs/qunit-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,14 @@ grunt.initConfig({
When using AMD to load QUnit and your tests, make sure to have a path for the `qunit` module defined.

## Events and reporting
[QUnit callback](http://api.qunitjs.com/category/callbacks/) methods and arguments are also emitted through grunt's event system so that you may build custom reporting tools. Please refer to to the QUnit documentation for more information.

The events, with arguments, are as follows:
QUnit events are forwarded to Grunt's event system, enabling you to build custom reporting tools. Please refer to the QUnit API documentation on [QUnit events](https://qunitjs.com/api/callbacks/QUnit.on/) and [QUnit callbacks](https://qunitjs.com/api/callbacks/) for when and what data is exposed from these events.

* `qunit.on.testStart` `(obj)`
* `qunit.on.testEnd` `(obj)`
* `qunit.on.runEnd` `(obj)`

* `qunit.begin`
* `qunit.moduleStart` `(name)`
* `qunit.testStart` `(name)`
* `qunit.log` `(result, actual, expected, message, source)`
* `qunit.testDone` `(name, failed, passed, total, duration)`
* `qunit.moduleDone` `(name, failed, passed, total)`
* `qunit.done` `(failed, passed, total, runtime)`
* `qunit.done`

In addition to forwarding QUnit's events, the following events are also emitted by the Grunt plugin:

Expand All @@ -127,4 +120,12 @@ You may listen for these events like so:
grunt.event.on('qunit.spawn', function (url) {
grunt.log.ok('Running test: ' + url);
});
grunt.event.on('qunit.on.testEnd', function (test) {
var name = test.fullName.join(' > ');
if (test.status === 'failed') {
grunt.log.error(name);
} else {
grunt.log.ok(name + ' # ' + test.status);
}
});
```
3 changes: 1 addition & 2 deletions tasks/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ module.exports = function(grunt) {
return failure;
}


// QUnit hooks.
// QUnit events.
eventBus.on('qunit.on.testStart', function(testStart) {
var name = testStart.fullName.join(' > ');
grunt.verbose.write(name + '...');
Expand Down

0 comments on commit 23c08c9

Please sign in to comment.