Browse Source

Strip ANSI escape codes in TAP test titles (#792)

browser-support
Juan 9 years ago
committed by Sindre Sorhus
parent
commit
3c4babd0ef
  1. 11
      lib/reporters/tap.js
  2. 2
      package.json
  3. 15
      test/reporters/tap.js

11
lib/reporters/tap.js

@ -1,5 +1,6 @@
'use strict';
var format = require('util').format;
var stripAnsi = require('strip-ansi');
// Parses stack trace and extracts original function name, file name and line.
function getSourceFromStack(stack, index) {
@ -36,10 +37,12 @@ TapReporter.prototype.test = function (test) {
directive = '# SKIP';
}
var title = stripAnsi(test.title);
if (test.error) {
output = [
'# ' + test.title,
format('not ok %d - %s', ++this.i, test.title),
'# ' + title,
format('not ok %d - %s', ++this.i, title),
' ---',
' operator: ' + test.error.operator,
' expected: ' + test.error.expected,
@ -49,8 +52,8 @@ TapReporter.prototype.test = function (test) {
];
} else {
output = [
'# ' + test.title,
format('%s %d - %s %s', passed, ++this.i, test.title, directive).trim()
'# ' + title,
format('%s %d - %s %s', passed, ++this.i, title, directive).trim()
];
}

2
package.json

@ -142,6 +142,7 @@
"slash": "^1.0.0",
"source-map-support": "^0.4.0",
"stack-utils": "^0.4.0",
"strip-ansi": "^3.0.1",
"strip-bom": "^2.0.0",
"time-require": "^0.1.2",
"unique-temp-dir": "^1.0.0",
@ -153,6 +154,7 @@
"delay": "^1.3.0",
"get-stream": "^2.0.0",
"git-branch": "^0.3.0",
"has-ansi": "^2.0.0",
"inquirer": "^1.0.2",
"lolex": "^1.4.0",
"mkdirp": "^0.5.1",

15
test/reporters/tap.js

@ -1,5 +1,7 @@
'use strict';
var test = require('tap').test;
var hasAnsi = require('has-ansi');
var chalk = require('chalk');
var tapReporter = require('../../lib/reporters/tap');
test('start', function (t) {
@ -179,3 +181,16 @@ test('skip test', function (t) {
t.is(actualOutput, expectedOutput);
t.end();
});
test('reporter strips ANSI characters', function (t) {
var reporter = tapReporter();
var output = reporter.test({
title: 'test ' + chalk.gray.dim('›') + ' my test',
type: 'test',
file: 'test.js'
});
t.notOk(hasAnsi(output.title));
t.end();
});

Loading…
Cancel
Save