* Fix XO linting and update some dev dependencies

Fixes #275

* Add some badges to the readme

* Tiny travis.yml tweak

* Require Node.js 6

* Validate the `level` option

Fixes #248

* Add failing test for #234 (#235)

* Add type definitions badge (#286)

* Add Tidelift mention in the readme

* Replace RawGit URL

Fixes #305

* Fix ignore chars regex flags in rainbow example (#306)

Use global matches rather than stopping after the first match.

* Strict mode in Flow definition (#309)

* Add security section

* Add docs comments and tests for TypeScript definitions (#299)

Fixes #293

* Update dependencies and meta tweaks

* Type definition improvements

* Enforce `chalk.constructor` to be called with `new` in TypeScript

* Add extra level/enabled property info in the readme (#308)

* Code style tweaks

* Change tagged template literal argument type to accept `unknown` instead of just `string` (#316)
This commit is contained in:
l198881 2021-05-26 15:23:19 -03:00 committed by GitHub
parent 9776a2ae5b
commit 029b69e482
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 760 additions and 479 deletions

View file

@ -9,10 +9,6 @@ chalk.constructor({level: 1});
new chalk.constructor({enabled: 'true'});
new chalk.constructor({enabled: true});
// $ExpectError (Can't pass in null)
chalk.underline(null);
chalk.underline('foo');
// $ExpectError (Can't have typo in chalk method)
chalk.rd('foo');
chalk.red('foo');
@ -29,10 +25,6 @@ chalk.red.bgBlue.underline('foo');
const badCtx = chalk.constructor({level: 4});
const ctx = chalk.constructor({level: 3});
// $ExpectError (Can't pass in null)
ctx(null);
ctx('foo');
// $ExpectError (Can't have typo in method name)
ctx.gry('foo');
ctx.grey('foo');

View file

@ -1,15 +1,15 @@
'use strict';
const resolveFrom = require('resolve-from');
module.exports = dir => {
require.cache[resolveFrom(dir, 'supports-color')] = {
exports: {
stdout: {
level: 3,
hasBasic: true,
has256: true,
has16m: true
}
}
};
const DEFAULT = {
stdout: {
level: 3,
hasBasic: true,
has256: true,
has16m: true
}
};
module.exports = (dir, override) => {
require.cache[resolveFrom(dir, 'supports-color')] = {exports: override || DEFAULT};
};

View file

@ -3,51 +3,50 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
console.log('TERM:', process.env.TERM || '[none]');
console.log('platform:', process.platform || '[unknown]');
test('don\'t add any styling when called as the base function', t => {
t.is(m('foo'), 'foo');
t.is(chalk('foo'), 'foo');
});
test('support multiple arguments in base function', t => {
t.is(m('hello', 'there'), 'hello there');
t.is(chalk('hello', 'there'), 'hello there');
});
test('style string', t => {
t.is(m.underline('foo'), '\u001B[4mfoo\u001B[24m');
t.is(m.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(m.bgRed('foo'), '\u001B[41mfoo\u001B[49m');
t.is(chalk.underline('foo'), '\u001B[4mfoo\u001B[24m');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(chalk.bgRed('foo'), '\u001B[41mfoo\u001B[49m');
});
test('support applying multiple styles at once', t => {
t.is(m.red.bgGreen.underline('foo'), '\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39m');
t.is(m.underline.red.bgGreen('foo'), '\u001B[4m\u001B[31m\u001B[42mfoo\u001B[49m\u001B[39m\u001B[24m');
t.is(chalk.red.bgGreen.underline('foo'), '\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39m');
t.is(chalk.underline.red.bgGreen('foo'), '\u001B[4m\u001B[31m\u001B[42mfoo\u001B[49m\u001B[39m\u001B[24m');
});
test('support nesting styles', t => {
t.is(
m.red('foo' + m.underline.bgBlue('bar') + '!'),
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
'\u001B[31mfoo\u001B[4m\u001B[44mbar\u001B[49m\u001B[24m!\u001B[39m'
);
});
test('support nesting styles of the same type (color, underline, bg)', t => {
t.is(
m.red('a' + m.yellow('b' + m.green('c') + 'b') + 'c'),
chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'),
'\u001B[31ma\u001B[33mb\u001B[32mc\u001B[33mb\u001B[31mc\u001B[39m'
);
});
test('reset all styles with `.reset()`', t => {
t.is(m.reset(m.red.bgGreen.underline('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
t.is(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
});
test('support caching multiple styles', t => {
const red = m.red;
const green = m.green;
const {red, green} = chalk.red;
const redBold = red.bold;
const greenBold = green.bold;
@ -57,44 +56,44 @@ test('support caching multiple styles', t => {
});
test('alias gray to grey', t => {
t.is(m.grey('foo'), '\u001B[90mfoo\u001B[39m');
t.is(chalk.grey('foo'), '\u001B[90mfoo\u001B[39m');
});
test('support variable number of arguments', t => {
t.is(m.red('foo', 'bar'), '\u001B[31mfoo bar\u001B[39m');
t.is(chalk.red('foo', 'bar'), '\u001B[31mfoo bar\u001B[39m');
});
test('support falsy values', t => {
t.is(m.red(0), '\u001B[31m0\u001B[39m');
t.is(chalk.red(0), '\u001B[31m0\u001B[39m');
});
test('don\'t output escape codes if the input is empty', t => {
t.is(m.red(), '');
t.is(m.red.blue.black(), '');
t.is(chalk.red(), '');
t.is(chalk.red.blue.black(), '');
});
test('keep Function.prototype methods', t => {
t.is(m.grey.apply(null, ['foo']), '\u001B[90mfoo\u001B[39m');
t.is(m.reset(m.red.bgGreen.underline.bind(null)('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
t.is(m.red.blue.black.call(null), '');
t.is(chalk.grey.apply(null, ['foo']), '\u001B[90mfoo\u001B[39m');
t.is(chalk.reset(chalk.red.bgGreen.underline.bind(null)('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
t.is(chalk.red.blue.black.call(null), '');
});
test('line breaks should open and close colors', t => {
t.is(m.grey('hello\nworld'), '\u001B[90mhello\u001B[39m\n\u001B[90mworld\u001B[39m');
t.is(chalk.grey('hello\nworld'), '\u001B[90mhello\u001B[39m\n\u001B[90mworld\u001B[39m');
});
test('properly convert RGB to 16 colors on basic color terminals', t => {
t.is(new m.constructor({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
t.is(new m.constructor({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
t.is(new chalk.constructor({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
t.is(new chalk.constructor({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
});
test('properly convert RGB to 256 colors on basic color terminals', t => {
t.is(new m.constructor({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
t.is(new m.constructor({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
t.is(new m.constructor({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m');
t.is(new chalk.constructor({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
t.is(new chalk.constructor({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
t.is(new chalk.constructor({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m');
});
test('don\'t emit RGB codes if level is 0', t => {
t.is(new m.constructor({level: 0}).hex('#FF0000')('hello'), 'hello');
t.is(new m.constructor({level: 0}).bgHex('#FF0000')('hello'), 'hello');
t.is(new chalk.constructor({level: 0}).hex('#FF0000')('hello'), 'hello');
t.is(new chalk.constructor({level: 0}).bgHex('#FF0000')('hello'), 'hello');
});

View file

@ -3,20 +3,32 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('create an isolated context where colors can be disabled (by level)', t => {
const ctx = new m.constructor({level: 0, enabled: true});
t.is(ctx.red('foo'), 'foo');
t.is(m.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.level = 2;
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({level: 0, enabled: true});
t.is(instance.red('foo'), 'foo');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.level = 2;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
});
test('create an isolated context where colors can be disabled (by enabled flag)', t => {
const ctx = new m.constructor({enabled: false});
t.is(ctx.red('foo'), 'foo');
t.is(m.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.enabled = true;
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({enabled: false});
t.is(instance.red('foo'), 'foo');
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.enabled = true;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
});
test('the `level` option should be a number from 0 to 3', t => {
/* eslint-disable no-new */
t.throws(() => {
new chalk.constructor({level: 10});
}, /should be an integer from 0 to 3/);
t.throws(() => {
new chalk.constructor({level: -1});
}, /should be an integer from 0 to 3/);
/* eslint-enable no-new */
});

View file

@ -3,33 +3,33 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('don\'t output colors when manually disabled', t => {
m.enabled = false;
t.is(m.red('foo'), 'foo');
m.enabled = true;
chalk.enabled = false;
t.is(chalk.red('foo'), 'foo');
chalk.enabled = true;
});
test('enable/disable colors based on overall chalk enabled property, not individual instances', t => {
m.enabled = false;
const red = m.red;
chalk.enabled = false;
const {red} = chalk;
t.false(red.enabled);
m.enabled = true;
chalk.enabled = true;
t.true(red.enabled);
m.enabled = true;
chalk.enabled = true;
});
test('propagate enable/disable changes from child colors', t => {
m.enabled = false;
const red = m.red;
chalk.enabled = false;
const {red} = chalk;
t.false(red.enabled);
t.false(m.enabled);
t.false(chalk.enabled);
red.enabled = true;
t.true(red.enabled);
t.true(m.enabled);
m.enabled = false;
t.true(chalk.enabled);
chalk.enabled = false;
t.false(red.enabled);
t.false(m.enabled);
m.enabled = true;
t.false(chalk.enabled);
chalk.enabled = true;
});

View file

@ -5,38 +5,38 @@ import execa from 'execa';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('don\'t output colors when manually disabled', t => {
const oldLevel = m.level;
m.level = 0;
t.is(m.red('foo'), 'foo');
m.level = oldLevel;
const oldLevel = chalk.level;
chalk.level = 0;
t.is(chalk.red('foo'), 'foo');
chalk.level = oldLevel;
});
test('enable/disable colors based on overall chalk enabled property, not individual instances', t => {
const oldLevel = m.level;
m.level = 1;
const red = m.red;
const oldLevel = chalk.level;
chalk.level = 1;
const {red} = chalk;
t.is(red.level, 1);
m.level = 0;
t.is(red.level, m.level);
m.level = oldLevel;
chalk.level = 0;
t.is(red.level, chalk.level);
chalk.level = oldLevel;
});
test('propagate enable/disable changes from child colors', t => {
const oldLevel = m.level;
m.level = 1;
const red = m.red;
const oldLevel = chalk.level;
chalk.level = 1;
const {red} = chalk;
t.is(red.level, 1);
t.is(m.level, 1);
t.is(chalk.level, 1);
red.level = 0;
t.is(red.level, 0);
t.is(m.level, 0);
m.level = 1;
t.is(chalk.level, 0);
chalk.level = 1;
t.is(red.level, 1);
t.is(m.level, 1);
m.level = oldLevel;
t.is(chalk.level, 1);
chalk.level = oldLevel;
});
test('disable colors if they are not supported', async t => {

16
test/no-color-support.js Normal file
View file

@ -0,0 +1,16 @@
import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname, {
level: 0,
hasBasic: false,
has256: false,
has16m: false
});
const chalk = require('..');
test.failing('colors can be forced by using chalk.enabled', t => {
chalk.enabled = true;
t.is(chalk.green('hello'), '\u001B[32mhello\u001B[39m');
});

View file

@ -4,82 +4,82 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('return an empty string for an empty literal', t => {
const ctx = m.constructor();
t.is(ctx``, '');
const instance = chalk.constructor();
t.is(instance``, '');
});
test('return a regular string for a literal with no templates', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`hello`, 'hello');
const instance = chalk.constructor({level: 0});
t.is(instance`hello`, 'hello');
});
test('correctly perform template parsing', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`{bold Hello, {cyan World!} This is a} test. {green Woo!}`,
ctx.bold('Hello,', ctx.cyan('World!'), 'This is a') + ' test. ' + ctx.green('Woo!'));
const instance = chalk.constructor({level: 0});
t.is(instance`{bold Hello, {cyan World!} This is a} test. {green Woo!}`,
instance.bold('Hello,', instance.cyan('World!'), 'This is a') + ' test. ' + instance.green('Woo!'));
});
test('correctly perform template substitutions', t => {
const ctx = m.constructor({level: 0});
const instance = chalk.constructor({level: 0});
const name = 'Sindre';
const exclamation = 'Neat';
t.is(ctx`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`,
ctx.bold('Hello,', ctx.cyan.inverse(name + '!'), 'This is a') + ' test. ' + ctx.green(exclamation + '!'));
t.is(instance`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`,
instance.bold('Hello,', instance.cyan.inverse(name + '!'), 'This is a') + ' test. ' + instance.green(exclamation + '!'));
});
test('correctly parse and evaluate color-convert functions', t => {
const ctx = m.constructor({level: 3});
t.is(ctx`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
const instance = chalk.constructor({level: 3});
t.is(instance`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
'\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, ' +
'\u001B[27m\u001B[39m\u001B[22m\u001B[1m' +
'\u001B[38;2;144;10;178mthere!\u001B[39m\u001B[22m');
t.is(ctx`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`,
t.is(instance`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`,
'\u001B[1m\u001B[48;2;144;10;178m\u001B[7mHello, ' +
'\u001B[27m\u001B[49m\u001B[22m\u001B[1m' +
'\u001B[48;2;144;10;178mthere!\u001B[49m\u001B[22m');
});
test('properly handle escapes', t => {
const ctx = m.constructor({level: 3});
t.is(ctx`{bold hello \{in brackets\}}`,
const instance = chalk.constructor({level: 3});
t.is(instance`{bold hello \{in brackets\}}`,
'\u001B[1mhello {in brackets}\u001B[22m');
});
test('throw if there is an unclosed block', t => {
const ctx = m.constructor({level: 3});
const instance = chalk.constructor({level: 3});
try {
console.log(ctx`{bold this shouldn't appear ever\}`);
console.log(instance`{bold this shouldn't appear ever\}`);
t.fail();
} catch (err) {
t.is(err.message, 'Chalk template literal is missing 1 closing bracket (`}`)');
} catch (error) {
t.is(error.message, 'Chalk template literal is missing 1 closing bracket (`}`)');
}
try {
console.log(ctx`{bold this shouldn't {inverse appear {underline ever\} :) \}`);
console.log(instance`{bold this shouldn't {inverse appear {underline ever\} :) \}`);
t.fail();
} catch (err) {
t.is(err.message, 'Chalk template literal is missing 3 closing brackets (`}`)');
} catch (error) {
t.is(error.message, 'Chalk template literal is missing 3 closing brackets (`}`)');
}
});
test('throw if there is an invalid style', t => {
const ctx = m.constructor({level: 3});
const instance = chalk.constructor({level: 3});
try {
console.log(ctx`{abadstylethatdoesntexist this shouldn't appear ever}`);
console.log(instance`{abadstylethatdoesntexist this shouldn't appear ever}`);
t.fail();
} catch (err) {
t.is(err.message, 'Unknown Chalk style: abadstylethatdoesntexist');
} catch (error) {
t.is(error.message, 'Unknown Chalk style: abadstylethatdoesntexist');
}
});
test('properly style multiline color blocks', t => {
const ctx = m.constructor({level: 3});
const instance = chalk.constructor({level: 3});
t.is(
ctx`{bold
instance`{bold
Hello! This is a
${'multiline'} block!
:)
@ -97,74 +97,74 @@ test('properly style multiline color blocks', t => {
});
test('escape interpolated values', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`Hello {bold hi}`, 'Hello hi');
t.is(ctx`Hello ${'{bold hi}'}`, 'Hello {bold hi}');
const instance = chalk.constructor({level: 0});
t.is(instance`Hello {bold hi}`, 'Hello hi');
t.is(instance`Hello ${'{bold hi}'}`, 'Hello {bold hi}');
});
test('allow custom colors (themes) on custom contexts', t => {
const ctx = m.constructor({level: 3});
ctx.rose = ctx.hex('#F6D9D9');
t.is(ctx`Hello, {rose Rose}.`, 'Hello, \u001B[38;2;246;217;217mRose\u001B[39m.');
const instance = chalk.constructor({level: 3});
instance.rose = instance.hex('#F6D9D9');
t.is(instance`Hello, {rose Rose}.`, 'Hello, \u001B[38;2;246;217;217mRose\u001B[39m.');
});
test('correctly parse newline literals (bug #184)', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`Hello
const instance = chalk.constructor({level: 0});
t.is(instance`Hello
{red there}`, 'Hello\nthere');
});
test('correctly parse newline escapes (bug #177)', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`Hello\nthere!`, `Hello\nthere!`);
const instance = chalk.constructor({level: 0});
t.is(instance`Hello\nthere!`, 'Hello\nthere!');
});
test('correctly parse escape in parameters (bug #177 comment 318622809)', t => {
const ctx = m.constructor({level: 0});
const instance = chalk.constructor({level: 0});
const str = '\\';
t.is(ctx`{blue ${str}}`, '\\');
t.is(instance`{blue ${str}}`, '\\');
});
test('correctly parses unicode/hex escapes', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`\u0078ylophones are fo\x78y! {magenta.inverse \u0078ylophones are fo\x78y!}`,
const instance = chalk.constructor({level: 0});
t.is(instance`\u0078ylophones are fo\x78y! {magenta.inverse \u0078ylophones are fo\x78y!}`,
'xylophones are foxy! xylophones are foxy!');
});
test('correctly parses string arguments', t => {
const ctx = m.constructor({level: 3});
t.is(ctx`{keyword('black').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
t.is(ctx`{keyword('blac\x6B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
t.is(ctx`{keyword('blac\u006B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
const instance = chalk.constructor({level: 3});
t.is(instance`{keyword('black').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
t.is(instance`{keyword('blac\x6B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
t.is(instance`{keyword('blac\u006B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
});
test('throws if a bad argument is encountered', t => {
const ctx = m.constructor({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
const instance = chalk.constructor({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
try {
console.log(ctx`{keyword(????) hi}`);
console.log(instance`{keyword(????) hi}`);
t.fail();
} catch (err) {
t.is(err.message, 'Invalid Chalk template style argument: ???? (in style \'keyword\')');
} catch (error) {
t.is(error.message, 'Invalid Chalk template style argument: ???? (in style \'keyword\')');
}
});
test('throws if an extra unescaped } is found', t => {
const ctx = m.constructor({level: 0});
const instance = chalk.constructor({level: 0});
try {
console.log(ctx`{red hi!}}`);
console.log(instance`{red hi!}}`);
t.fail();
} catch (err) {
t.is(err.message, 'Found extraneous } in Chalk template literal');
} catch (error) {
t.is(error.message, 'Found extraneous } in Chalk template literal');
}
});
test('should not parse upper-case escapes', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000a`, 'N\nT\tX07\x07U000A\u000AU000a\u000A');
const instance = chalk.constructor({level: 0});
t.is(instance`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000a`, 'N\nT\tX07\x07U000A\u000AU000a\u000A');
});
test('should properly handle undefined template interpolated values', t => {
const ctx = m.constructor({level: 0});
t.is(ctx`hello ${undefined}`, 'hello undefined');
t.is(ctx`hello ${null}`, 'hello null');
const instance = chalk.constructor({level: 0});
t.is(instance`hello ${undefined}`, 'hello undefined');
t.is(instance`hello ${null}`, 'hello null');
});

View file

@ -3,45 +3,45 @@ import test from 'ava';
// Spoof supports-color
require('./_supports-color')(__dirname);
const m = require('..');
const chalk = require('..');
test('visible: normal output when enabled', t => {
const ctx = new m.constructor({level: 3, enabled: true});
t.is(ctx.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({level: 3, enabled: true});
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
});
test('visible: no output when disabled', t => {
const ctx = new m.constructor({level: 3, enabled: false});
t.is(ctx.red.visible('foo'), '');
t.is(ctx.visible.red('foo'), '');
const instance = new chalk.constructor({level: 3, enabled: false});
t.is(instance.red.visible('foo'), '');
t.is(instance.visible.red('foo'), '');
});
test('visible: no output when level is too low', t => {
const ctx = new m.constructor({level: 0, enabled: true});
t.is(ctx.visible.red('foo'), '');
t.is(ctx.red.visible('foo'), '');
const instance = new chalk.constructor({level: 0, enabled: true});
t.is(instance.visible.red('foo'), '');
t.is(instance.red.visible('foo'), '');
});
test('test switching back and forth between enabled and disabled', t => {
const ctx = new m.constructor({level: 3, enabled: true});
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible('foo'), 'foo');
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
const instance = new chalk.constructor({level: 3, enabled: true});
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.visible('foo'), 'foo');
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
ctx.enabled = false;
t.is(ctx.red('foo'), 'foo');
t.is(ctx.visible('foo'), '');
t.is(ctx.visible.red('foo'), '');
t.is(ctx.red.visible('foo'), '');
t.is(ctx.red('foo'), 'foo');
instance.enabled = false;
t.is(instance.red('foo'), 'foo');
t.is(instance.visible('foo'), '');
t.is(instance.visible.red('foo'), '');
t.is(instance.red.visible('foo'), '');
t.is(instance.red('foo'), 'foo');
ctx.enabled = true;
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
t.is(ctx.visible('foo'), 'foo');
t.is(ctx.red('foo'), '\u001B[31mfoo\u001B[39m');
instance.enabled = true;
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
t.is(instance.visible('foo'), 'foo');
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
});

View file

@ -28,36 +28,36 @@ test.beforeEach(() => {
test('detect a simple term if TERM isn\'t set', t => {
delete process.env.TERM;
const m = importFresh('..');
t.is(m.blue('foo'), '\u001B[94mfoo\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue('foo'), '\u001B[94mfoo\u001B[39m');
});
test('replace blue foreground color in cmd.exe', t => {
process.env.TERM = 'dumb';
const m = importFresh('..');
t.is(m.blue('foo'), '\u001B[94mfoo\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue('foo'), '\u001B[94mfoo\u001B[39m');
});
test('don\'t replace blue foreground color in xterm based terminals', t => {
process.env.TERM = 'xterm-256color';
const m = importFresh('..');
t.is(m.blue('foo'), '\u001B[34mfoo\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue('foo'), '\u001B[34mfoo\u001B[39m');
});
test('don\'t apply dimmed styling on gray strings, see https://github.com/chalk/chalk/issues/58', t => {
process.env.TERM = 'dumb';
const m = importFresh('..');
t.is(m.gray.dim('foo'), '\u001B[90mfoo\u001B[22m\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.gray.dim('foo'), '\u001B[90mfoo\u001B[22m\u001B[39m');
});
test('apply dimmed styling on xterm compatible terminals', t => {
process.env.TERM = 'xterm';
const m = importFresh('..');
t.is(m.gray.dim('foo'), '\u001B[90m\u001B[2mfoo\u001B[22m\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.gray.dim('foo'), '\u001B[90m\u001B[2mfoo\u001B[22m\u001B[39m');
});
test('apply dimmed styling on strings of other colors', t => {
process.env.TERM = 'dumb';
const m = importFresh('..');
t.is(m.blue.dim('foo'), '\u001B[94m\u001B[2mfoo\u001B[22m\u001B[39m');
const chalk = importFresh('..');
t.is(chalk.blue.dim('foo'), '\u001B[94m\u001B[2mfoo\u001B[22m\u001B[39m');
});