Skip to content

Commit abeac18

Browse files
committed
Tests: Run TapReporter independently of stdout.isTTY or env.FORCE_COLOR
Tests should pass even when running in a subshell or otherwise without TTY or FORCE_COLOR=1.
1 parent cd8b8df commit abeac18

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

test/cli/TapReporter-to-TapParser.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ QUnit.module('TapReporter-to-TapParser', hooks => {
55
let emitter;
66
let buffer = '';
77

8+
function stripAsciEscapes (text) {
9+
// eslint-disable-next-line no-control-regex
10+
return text.replace(/\x1b\[[0-9]+m/g, '');
11+
}
12+
813
function log (str) {
9-
buffer += str + '\n';
14+
// Test independently of stdout.isTTY or env.FORCE_COLOR
15+
buffer += stripAsciEscapes(str) + '\n';
1016
}
1117

1218
async function getParseResult () {
@@ -99,7 +105,7 @@ QUnit.module('TapReporter-to-TapParser', hooks => {
99105
failures: [
100106
{
101107
ok: false,
102-
name: '\u001b[31mexample\u001b[39m',
108+
name: 'example',
103109
diag: {
104110
message: 'equal',
105111
severity: 'failed',
@@ -150,7 +156,7 @@ QUnit.module('TapReporter-to-TapParser', hooks => {
150156
failures: [
151157
{
152158
ok: false,
153-
name: '\u001b[31mexample\u001b[39m',
159+
name: 'example',
154160
diag: {
155161
message: 'deepEqual',
156162
severity: 'failed',
@@ -202,13 +208,13 @@ QUnit.module('TapReporter-to-TapParser', hooks => {
202208
},
203209
skips: [{
204210
ok: true,
205-
name: '\u001b[33mhello\u001b[39m',
211+
name: 'hello',
206212
skip: true,
207213
todo: false
208214
}],
209215
todos: [{
210216
ok: false,
211-
name: '\u001b[36mworld\u001b[39m',
217+
name: 'world',
212218
skip: false,
213219
todo: true
214220
}]

test/main/TapReporter.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,15 @@ QUnit.module('TapReporter', function (hooks) {
2626
var emitter;
2727
var last;
2828
var buffer;
29-
var kleur = {
30-
red: function (txt) {
31-
return '\x1B[31m' + txt + '\x1B[39m';
32-
},
33-
yellow: function (txt) {
34-
return '\x1B[33m' + txt + '\x1B[39m';
35-
},
36-
cyan: function (txt) {
37-
return '\x1B[36m' + txt + '\x1B[39m';
38-
},
39-
grey: function (txt) {
40-
return '\x1B[90m' + txt + '\x1B[39m';
41-
}
42-
};
29+
30+
function stripAsciEscapes (text) {
31+
// eslint-disable-next-line no-control-regex
32+
return text.replace(/\x1b\[[0-9]+m/g, '');
33+
}
4334

4435
function log (str) {
36+
// Test independently of stdout.isTTY or env.FORCE_COLOR
37+
str = stripAsciEscapes(str);
4538
buffer += str + '\n';
4639
last = str;
4740
}
@@ -88,7 +81,7 @@ QUnit.module('TapReporter', function (hooks) {
8881
});
8982

9083
QUnit.test('output ok for a skipped test', function (assert) {
91-
var expected = 'ok 1 ' + kleur.yellow('name') + ' # SKIP';
84+
var expected = 'ok 1 name # SKIP';
9285

9386
emitter.emit('testEnd', {
9487
name: 'name',
@@ -103,7 +96,7 @@ QUnit.module('TapReporter', function (hooks) {
10396
});
10497

10598
QUnit.test('output not ok for a todo test', function (assert) {
106-
var expected = 'not ok 1 ' + kleur.cyan('name') + ' # TODO';
99+
var expected = 'not ok 1 name # TODO';
107100

108101
emitter.emit('testEnd', {
109102
name: 'name',
@@ -118,7 +111,7 @@ QUnit.module('TapReporter', function (hooks) {
118111
});
119112

120113
QUnit.test('output not ok for a failing test', function (assert) {
121-
var expected = 'not ok 1 ' + kleur.red('name');
114+
var expected = 'not ok 1 name';
122115

123116
emitter.emit('testEnd', {
124117
name: 'name',
@@ -146,21 +139,21 @@ QUnit.module('TapReporter', function (hooks) {
146139
assertions: []
147140
});
148141

149-
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('name') + '\n'
142+
assert.strictEqual(buffer, 'not ok 1 name\n'
150143
+ ' ---\n'
151144
+ ' message: first error\n'
152145
+ ' severity: failed\n'
153146
+ ' stack: |\n'
154147
+ ' at Object.<anonymous> (/dev/null/test/unit/data.js:6:5)\n'
155-
+ ' ' + kleur.grey(' at require (node:internal/helpers.js:22:18)') + '\n'
148+
+ ' at require (node:internal/helpers.js:22:18)\n'
156149
+ ' at /dev/null/src/example/foo.js:220:27\n'
157150
+ ' ...\n'
158151
+ ' ---\n'
159152
+ ' message: second error\n'
160153
+ ' severity: failed\n'
161154
+ ' stack: |\n'
162155
+ ' at Object.<anonymous> (/dev/null/test/unit/data.js:6:5)\n'
163-
+ ' ' + kleur.grey(' at require (node:internal/helpers.js:22:18)') + '\n'
156+
+ ' at require (node:internal/helpers.js:22:18)\n'
164157
+ ' at /dev/null/src/example/foo.js:220:27\n'
165158
+ ' ...\n'
166159
);
@@ -171,7 +164,7 @@ QUnit.module('TapReporter', function (hooks) {
171164
emitter.clear();
172165
emitter.emit('error', 'Boo');
173166

174-
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('global failure') + '\n'
167+
assert.strictEqual(buffer, 'not ok 1 global failure\n'
175168
+ ' ---\n'
176169
+ ' message: Boo\n'
177170
+ ' severity: failed\n'
@@ -187,13 +180,13 @@ QUnit.module('TapReporter', function (hooks) {
187180
emitter.clear();
188181
emitter.emit('error', err);
189182

190-
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('global failure') + '\n'
183+
assert.strictEqual(buffer, 'not ok 1 global failure\n'
191184
+ ' ---\n'
192185
+ ' message: ReferenceError: Boo is not defined\n'
193186
+ ' severity: failed\n'
194187
+ ' stack: |\n'
195188
+ ' at Object.<anonymous> (/dev/null/test/unit/data.js:6:5)\n'
196-
+ ' ' + kleur.grey(' at require (node:internal/helpers.js:22:18)') + '\n'
189+
+ ' at require (node:internal/helpers.js:22:18)\n'
197190
+ ' at /dev/null/src/example/foo.js:220:27\n'
198191
+ ' ...\n'
199192
+ 'Bail out! ReferenceError: Boo is not defined\n'
@@ -440,7 +433,7 @@ QUnit.module('TapReporter', function (hooks) {
440433
});
441434

442435
assert.strictEqual(buffer, '1..6\n'
443-
+ '# pass 3\n# ' + kleur.yellow('skip 1') + '\n# ' + kleur.cyan('todo 0') + '\n# ' + kleur.red('fail 2') + '\n'
436+
+ '# pass 3\n# skip 1\n# todo 0\n# fail 2\n'
444437
);
445438
});
446439
});

0 commit comments

Comments
 (0)