Skip to content

Commit

Permalink
CLI: Fix TAP compliance for early errors (e.g. syntax error in test f…
Browse files Browse the repository at this point in the history
…ile)

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.
  • Loading branch information
Krinkle committed Jan 26, 2025
1 parent b4d48fc commit 01f7780
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/reporters/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export default class TapReporter {
this.log = options.log || Function.prototype.bind.call(console.log, console);

this.testCount = 0;
this.started = false;
this.ended = false;
this.bailed = false;

Expand All @@ -197,7 +198,10 @@ export default class TapReporter {
}

onRunStart (_runSuite) {
this.log('TAP version 13');
if (!this.started) {
this.log('TAP version 13');
this.started = true;
}
}

onError (error) {
Expand All @@ -210,6 +214,7 @@ export default class TapReporter {
// Imitate onTestEnd
// Skip this if we're past "runEnd" as it would look odd
if (!this.ended) {
this.onRunStart();
this.testCount = this.testCount + 1;
this.log(`not ok ${this.testCount} ${kleur.red('global failure')}`);
this.logError(error);
Expand Down
9 changes: 9 additions & 0 deletions test/cli/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ QUnit.module('TapReporter', hooks => {

hooks.beforeEach(function () {
emitter = new EventEmitter();
emitter.clear = function () {
buffer = '';
};
last = undefined;
buffer = '';
QUnit.reporters.tap.init(emitter, {
Expand Down Expand Up @@ -146,6 +149,9 @@ QUnit.module('TapReporter', hooks => {
});

QUnit.test('output global failure (string)', assert => {
emitter.emit('runStart');
emitter.clear();

emitter.emit('error', 'Boo');

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

QUnit.test('output global failure (Error)', assert => {
emitter.emit('runStart');
emitter.clear();

const err = new ReferenceError('Boo is not defined');
mockStack(err);
emitter.emit('error', err);
Expand Down
3 changes: 1 addition & 2 deletions test/cli/fixtures/syntax-error.tap.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# name: load file with syntax error
# command: ["qunit", "syntax-error.js"]

TAP version 13
not ok 1 global failure
---
message: |+
Expand All @@ -13,15 +14,13 @@ not ok 1 global failure
at internal
...
Bail out! Error: Failed to load file syntax-error.js
TAP version 13
not ok 2 global failure
---
message: No tests were run.
severity: failed
stack: |
Error: No tests were run.
at qunit.js
at internal
...
1..2
# pass 0
Expand Down
3 changes: 1 addition & 2 deletions test/cli/fixtures/unhandled-rejection.tap.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# name: unhandled rejection
# command: ["qunit","unhandled-rejection.js"]

TAP version 13
not ok 1 global failure
---
message: Error: outside of a test context
Expand All @@ -13,15 +14,13 @@ not ok 1 global failure
at internal
...
Bail out! Error: outside of a test context
TAP version 13
not ok 2 Unhandled Rejections > test passes just fine, but has a rejected promise
---
message: global failure: Error: Error thrown in non-returned promise!
severity: failed
stack: |
Error: Error thrown in non-returned promise!
at /qunit/test/cli/fixtures/unhandled-rejection.js:10:13
at internal
...
1..2
# pass 0
Expand Down

0 comments on commit 01f7780

Please sign in to comment.