Skip to content

Commit

Permalink
CLI: Limit colors in TAP reporter to test names
Browse files Browse the repository at this point in the history
Cherry-picked from a006064 (3.0.0-dev).

> Fixes compatibility with the popular `tap-parser`
> package which doesn't parse TAP lines correctly otherwise, since
> blocks like "not ok" must start on a new line, and with the color
> code in front, they are technically either a continuation of the
> previous block, or an ignored "Anything" line between two blocks.
>
> https://github.com/tapjs/tapjs/tree/ccfea67fec/src/parser
>
> https://testanything.org/tap-version-13-specification.html
>
> Ref #1801.

Includes a partial cherry-pick of 5812597 to clarify the local
name of the onRunEnd argument as `runEnd` instead of `runSuite`.
  • Loading branch information
Krinkle committed Dec 4, 2024
1 parent f5188cb commit 2cd038e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/reporters/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default class TapReporter {
// Skip this if we're past "runEnd" as it would look odd
if (!this.ended) {
this.testCount = this.testCount + 1;
this.log(kleur.red(`not ok ${this.testCount} global failure`));
this.log(`not ok ${this.testCount} ${kleur.red('global failure')}`);
this.logError(error);
}

Expand All @@ -222,29 +222,29 @@ export default class TapReporter {
this.log(`ok ${this.testCount} ${test.fullName.join(' > ')}`);
} else if (test.status === 'skipped') {
this.log(
kleur.yellow(`ok ${this.testCount} # SKIP ${test.fullName.join(' > ')}`)
`ok ${this.testCount} ${kleur.yellow(`# SKIP ${test.fullName.join(' > ')}`)}`
);
} else if (test.status === 'todo') {
this.log(
kleur.cyan(`not ok ${this.testCount} # TODO ${test.fullName.join(' > ')}`)
`not ok ${this.testCount} ${kleur.cyan(`# TODO ${test.fullName.join(' > ')}`)}`
);
test.errors.forEach((error) => this.logAssertion(error, 'todo'));
} else {
this.log(
kleur.red(`not ok ${this.testCount} ${test.fullName.join(' > ')}`)
`not ok ${this.testCount} ${kleur.red(test.fullName.join(' > '))}`
);
test.errors.forEach((error) => this.logAssertion(error));
}
}

onRunEnd (runSuite) {
onRunEnd (runEnd) {
this.ended = true;

this.log(`1..${runSuite.testCounts.total}`);
this.log(`# pass ${runSuite.testCounts.passed}`);
this.log(kleur.yellow(`# skip ${runSuite.testCounts.skipped}`));
this.log(kleur.cyan(`# todo ${runSuite.testCounts.todo}`));
this.log(kleur.red(`# fail ${runSuite.testCounts.failed}`));
this.log(`1..${runEnd.testCounts.total}`);
this.log(`# pass ${runEnd.testCounts.passed}`);
this.log(`# ${kleur.yellow(`skip ${runEnd.testCounts.skipped}`)}`);
this.log(`# ${kleur.cyan(`todo ${runEnd.testCounts.todo}`)}`);
this.log(`# ${kleur.red(`fail ${runEnd.testCounts.failed}`)}`);
}

logAssertion (error, severity) {
Expand Down
18 changes: 9 additions & 9 deletions test/cli/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ QUnit.module('TapReporter', hooks => {
});

QUnit.test('output ok for a skipped test', assert => {
const expected = kleur.yellow('ok 1 # SKIP name');
const expected = 'ok 1 ' + kleur.yellow('# SKIP name');

emitter.emit('testEnd', {
name: 'name',
Expand All @@ -81,7 +81,7 @@ QUnit.module('TapReporter', hooks => {
});

QUnit.test('output not ok for a todo test', assert => {
const expected = kleur.cyan('not ok 1 # TODO name');
const expected = 'not ok 1 ' + kleur.cyan('# TODO name');

emitter.emit('testEnd', {
name: 'name',
Expand All @@ -96,7 +96,7 @@ QUnit.module('TapReporter', hooks => {
});

QUnit.test('output not ok for a failing test', assert => {
const expected = kleur.red('not ok 1 name');
const expected = 'not ok 1 ' + kleur.red('name');

emitter.emit('testEnd', {
name: 'name',
Expand Down Expand Up @@ -124,7 +124,7 @@ QUnit.module('TapReporter', hooks => {
assertions: []
});

assert.strictEqual(buffer, `${kleur.red('not ok 1 name')}
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('name') + `
---
message: first error
severity: failed
Expand All @@ -148,7 +148,7 @@ QUnit.module('TapReporter', hooks => {
QUnit.test('output global failure (string)', assert => {
emitter.emit('error', 'Boo');

assert.strictEqual(buffer, `${kleur.red('not ok 1 global failure')}
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('global failure') + `
---
message: Boo
severity: failed
Expand All @@ -163,7 +163,7 @@ Bail out! Boo
mockStack(err);
emitter.emit('error', err);

assert.strictEqual(buffer, `${kleur.red('not ok 1 global failure')}
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('global failure') + `
---
message: ReferenceError: Boo is not defined
severity: failed
Expand Down Expand Up @@ -418,9 +418,9 @@ Bail out! ReferenceError: Boo is not defined

assert.strictEqual(buffer, `1..6
# pass 3
${kleur.yellow('# skip 1')}
${kleur.cyan('# todo 0')}
${kleur.red('# fail 2')}
# ${kleur.yellow('skip 1')}
# ${kleur.cyan('todo 0')}
# ${kleur.red('fail 2')}
`
);
});
Expand Down

0 comments on commit 2cd038e

Please sign in to comment.