Skip to content

Commit 385f2cd

Browse files
committed
Improve color support tests
1 parent 98dded5 commit 385f2cd

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

test/cli.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
'use strict';
2+
// tap runs tests in a non-TTY environment, that's why supports-color will return false.
3+
// Since we're going to check for colored output in our tests, we need to override that.
4+
process.env.FORCE_COLOR = true;
5+
26
const fs = require('fs');
37
const path = require('path');
48
const childProcess = require('child_process');
@@ -16,12 +20,6 @@ const colors = require('../lib/colors');
1620

1721
const cliPath = path.join(__dirname, '../cli.js');
1822

19-
// For some reason chalk is disabled by default
20-
chalk.enabled = true;
21-
for (const key of Object.keys(colors)) {
22-
colors[key].enabled = true;
23-
}
24-
2523
function execCli(args, opts, cb) {
2624
let dirname;
2725
let env;

test/fixture/chalk-disabled.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import chalk from 'chalk';
2+
import test from '../../';
3+
4+
test('should not support colors', t => {
5+
t.false(chalk.enabled);
6+
});

test/fixture/chalk-enabled.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import chalk from 'chalk';
2+
import test from '../../';
3+
4+
test('should support colors', t => {
5+
t.true(chalk.enabled);
6+
});

test/fork.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ const CachingPrecompiler = require('../lib/caching-precompiler');
77
const cacheDir = path.join(__dirname, '../node_modules/.cache/ava');
88
const precompiler = new CachingPrecompiler({path: cacheDir});
99

10-
function fork(testPath) {
10+
function fork(testPath, options) {
1111
const hash = precompiler.precompileFile(testPath);
1212
const precompiled = {};
1313
precompiled[testPath] = hash;
1414

15-
return _fork(testPath, {
15+
return _fork(testPath, Object.assign({
1616
cacheDir,
1717
precompiled
18-
});
18+
}, options));
1919
}
2020

2121
function fixture(name) {
@@ -125,3 +125,20 @@ test('babelrc is ignored', t => {
125125
t.end();
126126
});
127127
});
128+
129+
test('color support is initialized correctly', t => {
130+
t.plan(1);
131+
132+
return Promise.all([
133+
fork(fixture('chalk-enabled.js'), {color: true}).run({}),
134+
fork(fixture('chalk-disabled.js'), {color: false}).run({}),
135+
fork(fixture('chalk-disabled.js'), {}).run({})
136+
]).then(info => {
137+
info.forEach(info => {
138+
if (info.stats.failCount > 0) {
139+
throw new Error(`${info.file} failed`);
140+
}
141+
});
142+
t.is(info.length, 3);
143+
});
144+
});

0 commit comments

Comments
 (0)