Skip to content

Commit 27a33d1

Browse files
committed
Core: Add memory to the runEnd event to allow late event listeners
While this is not in any way a documented API, it is quite common for QUnit runners that are based on Selenium/Webdriver to use HTML scraping to obtain a simple summary of whether the run passed. Intruce memory for `runEnd` event so that anything JS-based has a stable and documented way to obtain the result in a machine-readable format, without needing to resort to HTML scraping. One common reason sometimes avoid the event emitter is that, depending on various circumstances, a custom test runner might end up injecting code slightly too late, at which point JS code to listen for our event might not fire, whereas the DOM is still available. This is rare, but can happen: * if the file is served as-is. browserstack-runner and QTap solves this by proxying the HTML file and inject an inline script to reliable listen early. Karma solves this by proxing the test framework instead (qunit.js) and adapting it to include relevant event listeners upfront. * and, if the browser is driven without early JS control. When WebDriver v1 is used, something like webdriver "execute" is not guruanteed to run before DOM-ready or window.onload. If the Node.js process is slow or far away from the browser (e.g. cloud), and if the test suite is relatively small/fast, then the injected code might arrive after the tests have already finished. grunt-contrib-qunit avoids this by using Puppeteer and its `Page.evaluateOnNewDocument()` method, to reliably run a script before any others. * and, if the test is relatively small/fast. Example at mauriciolauffer/wdio-qunit-service#13 Solve this once and for all by adding memory to the `runEnd` event. This allows late event listers to handle the event retroactively. Cherry-picked from 8f25f26 (3.0.0-dev).
1 parent b13ade0 commit 27a33d1

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

src/core/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ const config = {
131131
// started: 0,
132132

133133
// Internal state
134+
_event_listeners: Object.create(null),
135+
_event_memory: {},
134136
_deprecated_timeout_shown: false,
135137
_deprecated_countEachStep_shown: false,
136138
blocking: true,

src/events.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inArray } from './core/utilities';
2+
import config from './core/config';
23

3-
const LISTENERS = Object.create(null);
44
const SUPPORTED_EVENTS = [
55
'error',
66
'runStart',
@@ -11,6 +11,9 @@ const SUPPORTED_EVENTS = [
1111
'suiteEnd',
1212
'runEnd'
1313
];
14+
const MEMORY_EVENTS = [
15+
'runEnd'
16+
];
1417

1518
/**
1619
* Emits an event with the specified data to all currently registered listeners.
@@ -30,12 +33,16 @@ export function emit (eventName, data) {
3033
}
3134

3235
// Clone the callbacks in case one of them registers a new callback
33-
const originalCallbacks = LISTENERS[eventName];
36+
const originalCallbacks = config._event_listeners[eventName];
3437
const callbacks = originalCallbacks ? [...originalCallbacks] : [];
3538

3639
for (let i = 0; i < callbacks.length; i++) {
3740
callbacks[i](data);
3841
}
42+
43+
if (inArray(MEMORY_EVENTS, eventName)) {
44+
config._event_memory[eventName] = data;
45+
}
3946
}
4047

4148
/**
@@ -57,12 +64,14 @@ export function on (eventName, callback) {
5764
throw new TypeError('callback must be a function when registering a listener');
5865
}
5966

60-
if (!LISTENERS[eventName]) {
61-
LISTENERS[eventName] = [];
62-
}
67+
const listeners = config._event_listeners[eventName] || (config._event_listeners[eventName] = []);
6368

6469
// Don't register the same callback more than once
65-
if (!inArray(callback, LISTENERS[eventName])) {
66-
LISTENERS[eventName].push(callback);
70+
if (!inArray(callback, listeners)) {
71+
listeners.push(callback);
72+
73+
if (config._event_memory[eventName] !== undefined) {
74+
callback(config._event_memory[eventName]);
75+
}
6776
}
6877
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
QUnit.on('runEnd', function (run) {
2+
console.log(`# early runEnd total=${run.testCounts.total} passed=${run.testCounts.passed} failed=${run.testCounts.failed}`);
3+
setTimeout(function () {
4+
QUnit.on('runEnd', function (run) {
5+
console.log(`# late runEnd total=${run.testCounts.total} passed=${run.testCounts.passed} failed=${run.testCounts.failed}`);
6+
});
7+
});
8+
});
9+
10+
QUnit.module('First', function () {
11+
QUnit.test('A', function (assert) {
12+
assert.true(true);
13+
});
14+
QUnit.test('B', function (assert) {
15+
assert.true(false);
16+
});
17+
});
18+
19+
QUnit.module('Second', function () {
20+
QUnit.test('C', function (assert) {
21+
assert.true(true);
22+
});
23+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# command: ["qunit", "event-runEnd-memory.js"]
2+
3+
TAP version 13
4+
ok 1 First > A
5+
not ok 2 First > B
6+
---
7+
message: failed
8+
severity: failed
9+
actual : false
10+
expected: true
11+
stack: |
12+
at /qunit/test/cli/fixtures/event-runEnd-memory.js:15:16
13+
...
14+
ok 3 Second > C
15+
1..3
16+
# pass 2
17+
# skip 0
18+
# todo 0
19+
# fail 1
20+
# early runEnd total=3 passed=2 failed=1
21+
# late runEnd total=3 passed=2 failed=1
22+
23+
# exit code: 1

0 commit comments

Comments
 (0)