Skip to content

Commit a4cbc1c

Browse files
committed
Tests: Run TapReporter independently of stdout.isTTY or env.FORCE_COLOR
Cherry-picked from abeac18 (3.0.0-dev). > Tests should pass even when running in a subshell or otherwise > without TTY or FORCE_COLOR=1.
1 parent 7d0c65c commit a4cbc1c

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

test/cli/TapReporter-to-TapParser.js

Lines changed: 10 additions & 4 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',
@@ -205,7 +211,7 @@ QUnit.module('TapReporter-to-TapParser', hooks => {
205211
name: 'example'
206212
}, {
207213
ok: true,
208-
name: '\u001b[33mhello\u001b[39m'
214+
name: 'hello'
209215
}]
210216
});
211217
});

test/cli/TapReporter.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const kleur = require('kleur');
21
const { EventEmitter } = require('events');
32

43
function mockStack (error) {
@@ -29,7 +28,14 @@ QUnit.module('TapReporter', hooks => {
2928
let last;
3029
let buffer;
3130

31+
function stripAsciEscapes (text) {
32+
// eslint-disable-next-line no-control-regex
33+
return text.replace(/\x1b\[[0-9]+m/g, '');
34+
}
35+
3236
function log (str) {
37+
// Test independently of stdout.isTTY or env.FORCE_COLOR
38+
str = stripAsciEscapes(str);
3339
buffer += str + '\n';
3440
last = str;
3541
}
@@ -69,7 +75,7 @@ QUnit.module('TapReporter', hooks => {
6975
});
7076

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

7480
emitter.emit('testEnd', {
7581
name: 'name',
@@ -84,7 +90,7 @@ QUnit.module('TapReporter', hooks => {
8490
});
8591

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

8995
emitter.emit('testEnd', {
9096
name: 'name',
@@ -99,7 +105,7 @@ QUnit.module('TapReporter', hooks => {
99105
});
100106

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

104110
emitter.emit('testEnd', {
105111
name: 'name',
@@ -127,21 +133,21 @@ QUnit.module('TapReporter', hooks => {
127133
assertions: []
128134
});
129135

130-
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('name') + `
136+
assert.strictEqual(buffer, `not ok 1 name
131137
---
132138
message: first error
133139
severity: failed
134140
stack: |
135141
at Object.<anonymous> (/dev/null/test/unit/data.js:6:5)
136-
${kleur.grey(' at require (internal/helpers.js:22:18)')}
142+
at require (internal/helpers.js:22:18)
137143
at /dev/null/src/example/foo.js:220:27
138144
...
139145
---
140146
message: second error
141147
severity: failed
142148
stack: |
143149
at Object.<anonymous> (/dev/null/test/unit/data.js:6:5)
144-
${kleur.grey(' at require (internal/helpers.js:22:18)')}
150+
at require (internal/helpers.js:22:18)
145151
at /dev/null/src/example/foo.js:220:27
146152
...
147153
`
@@ -154,7 +160,7 @@ QUnit.module('TapReporter', hooks => {
154160

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

157-
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('global failure') + `
163+
assert.strictEqual(buffer, `not ok 1 global failure
158164
---
159165
message: Boo
160166
severity: failed
@@ -172,13 +178,13 @@ Bail out! Boo
172178
mockStack(err);
173179
emitter.emit('error', err);
174180

175-
assert.strictEqual(buffer, 'not ok 1 ' + kleur.red('global failure') + `
181+
assert.strictEqual(buffer, `not ok 1 global failure
176182
---
177183
message: ReferenceError: Boo is not defined
178184
severity: failed
179185
stack: |
180186
at Object.<anonymous> (/dev/null/test/unit/data.js:6:5)
181-
${kleur.grey(' at require (internal/helpers.js:22:18)')}
187+
at require (internal/helpers.js:22:18)
182188
at /dev/null/src/example/foo.js:220:27
183189
...
184190
Bail out! ReferenceError: Boo is not defined
@@ -427,9 +433,9 @@ Bail out! ReferenceError: Boo is not defined
427433

428434
assert.strictEqual(buffer, `1..6
429435
# pass 3
430-
# ${kleur.yellow('skip 1')}
431-
# ${kleur.cyan('todo 0')}
432-
# ${kleur.red('fail 2')}
436+
# skip 1
437+
# todo 0
438+
# fail 2
433439
`
434440
);
435441
});

0 commit comments

Comments
 (0)