Skip to content

Using chalk in AVA tests does not colorize the strings #1124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KnisterPeter opened this issue Nov 22, 2016 · 10 comments · Fixed by #1244
Closed

Using chalk in AVA tests does not colorize the strings #1124

KnisterPeter opened this issue Nov 22, 2016 · 10 comments · Fixed by #1244
Labels
bug current functionality does not work as desired

Comments

@KnisterPeter
Copy link

KnisterPeter commented Nov 22, 2016

I noticed that the following code does work as expected

const chalk = require('chalk');
console.log(chalk.blue('hello world')); // as expected the result is blue

but this doesn't

const test = require('ava');
const chalk = require('chalk');
console.log(chalk.blue('hello world')); // NOT expected the result is grey
test(t => t.fail());

I'm using ava 0.17.0 and chalk 1.1.3.

@sindresorhus
Copy link
Member

// @thinkimlazy

@thinkimlazy
Copy link

thinkimlazy commented Nov 23, 2016

Ye that what I meant. Mine test files with no colors, but my terminal supports color. Sindre got colors.
I'll dig into
#1104

@vadimdemedes vadimdemedes added the bug current functionality does not work as desired label Nov 26, 2016
@novemberborn
Copy link
Member

@sindresorhus @thinkimlazy what is the actual bug here? Is it AVA overriding the chalk settings on a global basis? That seems like an issue with chalks design more than anything.

@unional
Copy link

unional commented Jan 10, 2017

I figure out the problem. It's in the ansi-styles. This issue happens on windows and ansi-styles does not produce the correct string in that environment.

Windows follow this (or something similar, up to Windows 8):
http://www.termsys.demon.co.uk/vtansi.htm

@unional
Copy link

unional commented Jan 10, 2017

@Qix-
Copy link
Contributor

Qix- commented Feb 6, 2017

@unional We do literally the exact same thing. Nobody else has reported any issues on Windows until this bug. There's no difference here. This is probably a configuration issue happening inside of AVA.

@unional
Copy link

unional commented Feb 6, 2017

Mentioned on the ansi-styles issue. It is not the same.

To see the difference:

npm i -g ava
npm i chalk ava aurelia-logging aurelia-logging-color
// index.js
var chalk = require('chalk')
var logging = require('aurelia-logging')
var color = require('aurelia-logging-color')
var test = require('ava')

logging.setLevel(logging.logLevel.debug)
var log = logging.getLogger('some log')
logging.addAppender(new color.ColorAppender())

test(_t => {
  console.log(chalk.bgGreen('suppose to be green'))
  log.debug('log something')
})
ava index.js

@Qix-
Copy link
Contributor

Qix- commented Feb 6, 2017

It is the same. Check supports-color level reports, here. Nobody has reported anything on windows other than in this ticket. This is an AVA issue until I see definitive reasoning for it being an ansi-styles problem.

@unional
Copy link

unional commented Feb 6, 2017

Ok. You are correct. I do see that supports-color turn chalk off in win32 environment.

When I worked on aurelia-logging-color, I can't use ansi-style because after bundling all dependencies the library become quite huge, and I recall seeing the color not rendered correctly. That's why I went in and go through the code and the material to figure out how to get it working in my case.

However, right now I can't reproduce the problem. Everything seems to be working fine for chalk, ansi-style and so on. I must be dreaming back then. 😛

As for the OP, I tried chalk.enabled = true to force coloring and it is working. FYI. 🌷

var chalk = require('chalk')
var test = require('ava')

chalk.enabled = true

test(_t => {
  console.log(`bgBlue: ${chalk.bgBlue('bgBlue')} and yellow: ${chalk.yellow('yellow')}`)
  console.log(chalk.bgCyan('cyan'), chalk.bgGreen('green'))
})

image

@sindresorhus
Copy link
Member

Closing as this is working now.

jhnns added a commit to jhnns/ava that referenced this issue Feb 8, 2017
'chalk' is using the 'supports-color' module which reads process.stdout.isTTY as soon as it is required. Since our process-adapter is initializing a fake TTY support, we must ensure that 'supports-color' is not executed before our fake TTY support is initialized.

avajs#1124
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug current functionality does not work as desired
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants