Skip to content

Commit 83c9499

Browse files
committed
CLI: Fix TAP compliance for early errors (e.g. syntax error in test file)
Cherry-picked from 62ca15a (3.0.0-dev). > Core: Convert "No tests were run." from fake test to error Early errors produced output like so: ``` not ok 1 global failure --- message: ReferenceError: boom is not defined stack: | ReferenceError: boom is not defined at /example.test.js ... Bail out! ReferenceError: boom is not defined TAP version 13 1..1 ``` Instead of: ``` TAP version 13 not ok 1 global failure --- message: ReferenceError: boom is not defined stack: | ReferenceError: boom is not defined at /example.test.js ... Bail out! ReferenceError: boom is not defined 1..1 ``` Because prior to the introduction of the "error" event in 2.17.0, the only failures were test failures, and those would not begin until after QUnit.begin() / "runStart" event.
1 parent b4d48fc commit 83c9499

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/reporters/TapReporter.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export default class TapReporter {
183183
this.log = options.log || Function.prototype.bind.call(console.log, console);
184184

185185
this.testCount = 0;
186+
this.started = false;
186187
this.ended = false;
187188
this.bailed = false;
188189

@@ -197,7 +198,10 @@ export default class TapReporter {
197198
}
198199

199200
onRunStart (_runSuite) {
200-
this.log('TAP version 13');
201+
if (!this.started) {
202+
this.log('TAP version 13');
203+
this.started = true;
204+
}
201205
}
202206

203207
onError (error) {
@@ -210,6 +214,7 @@ export default class TapReporter {
210214
// Imitate onTestEnd
211215
// Skip this if we're past "runEnd" as it would look odd
212216
if (!this.ended) {
217+
this.onRunStart();
213218
this.testCount = this.testCount + 1;
214219
this.log(`not ok ${this.testCount} ${kleur.red('global failure')}`);
215220
this.logError(error);

test/cli/TapReporter.js

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ QUnit.module('TapReporter', hooks => {
3636

3737
hooks.beforeEach(function () {
3838
emitter = new EventEmitter();
39+
emitter.clear = function () {
40+
buffer = '';
41+
};
3942
last = undefined;
4043
buffer = '';
4144
QUnit.reporters.tap.init(emitter, {
@@ -146,6 +149,9 @@ QUnit.module('TapReporter', hooks => {
146149
});
147150

148151
QUnit.test('output global failure (string)', assert => {
152+
emitter.emit('runStart');
153+
emitter.clear();
154+
149155
emitter.emit('error', 'Boo');
150156

151157
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('global failure') + `
@@ -159,6 +165,9 @@ Bail out! Boo
159165
});
160166

161167
QUnit.test('output global failure (Error)', assert => {
168+
emitter.emit('runStart');
169+
emitter.clear();
170+
162171
const err = new ReferenceError('Boo is not defined');
163172
mockStack(err);
164173
emitter.emit('error', err);

test/cli/fixtures/syntax-error.tap.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# name: load file with syntax error
22
# command: ["qunit", "syntax-error.js"]
33

4+
TAP version 13
45
not ok 1 global failure
56
---
67
message: |+
@@ -13,7 +14,6 @@ not ok 1 global failure
1314
at internal
1415
...
1516
Bail out! Error: Failed to load file syntax-error.js
16-
TAP version 13
1717
not ok 2 global failure
1818
---
1919
message: No tests were run.

test/cli/fixtures/unhandled-rejection.tap.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# name: unhandled rejection
22
# command: ["qunit","unhandled-rejection.js"]
33

4+
TAP version 13
45
not ok 1 global failure
56
---
67
message: Error: outside of a test context
@@ -13,15 +14,13 @@ not ok 1 global failure
1314
at internal
1415
...
1516
Bail out! Error: outside of a test context
16-
TAP version 13
1717
not ok 2 Unhandled Rejections > test passes just fine, but has a rejected promise
1818
---
1919
message: global failure: Error: Error thrown in non-returned promise!
2020
severity: failed
2121
stack: |
2222
Error: Error thrown in non-returned promise!
2323
at /qunit/test/cli/fixtures/unhandled-rejection.js:10:13
24-
at internal
2524
...
2625
1..2
2726
# pass 0

0 commit comments

Comments
 (0)