avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

(3.0.0-beta.2) behavior change on snapshots with ansi control chars #2367

Closed sramam closed 4 years ago

sramam commented 4 years ago

Description

The behavior of how chalk'd output is handled has changed w.r.t. snapshots. Took me a while to pin this on 3.0.0.

Use case: I snapshot the CLI output as an easy way to track changes over time. This includes all Ansi-control chars. v3 snapshots seem to strip all ansi-control characters from snapshots.

I did not see an obvious cause in the code, but please do not remove this ability, it will cause me a lot of grief.

Test Source

Test case

const test = require("ava");
const chalk = require("chalk");

test(`color`, (t) => {
    t.snapshot(chalk.dim.red("some colorful text"));
})

Results in a snapshot mismatch.

image

Snapshot.md - version 2

# Snapshot report for `test.js`

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).

## color

> Snapshot 1

    'some colorful text'

Snapshot.md - version 3

# Snapshot report for `test.js`

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).

## color

> Snapshot 1

    'some colorful text'
novemberborn commented 4 years ago

The "TTY"-nesh of a worker process no longer mimicks the environment you start AVA in. This proved too difficult to maintain.

https://github.com/avajs/ava/issues/2343 discusses publishing our fake implementation as its own package, which you could then load.

Alternatively you can configure a Chalk instance to force colors to be available.


I'd just like to say, thank you for putting the v3 beta through its paces!

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

sramam commented 4 years ago

Alternatively you can configure a Chalk instance to force colors to be available.

Could you explain how to do this? I'm looking for the quickest win in the moment.

novemberborn commented 4 years ago

Assuming latest chalk:

const { Instance } = require('chalk')
const chalk = new Instance({level: 3})
sramam commented 4 years ago

Oh - I get this - but where - in each test suite? in lib/concordance-options.js (v3.0), I logged the level

console.log(`chalk: ${Math.max(chalk.level, 1)} forceColor.level: ${forceColor.level}`);

and it is being reported as 3. image

novemberborn commented 4 years ago

No the chalk you're using in your test.

sramam commented 4 years ago

ah! This seems to be a side-effect of upgrading chalk!

Not sure I understand why the default level of chalk is getting set to zero - but that's a discussion for another day on another repo.


I'd also like you thank you for a wonderful test runner and all the work you do to move it forward. My value-add to the beta was incidental - I just have a complex application with a lot of tests, so a simple migration kept surfacing issues. Your effort requires dedication to the cause and is deeply appreciated!.

novemberborn commented 4 years ago

ah! This seems to be a side-effect of upgrading chalk!

Not sure I understand why the default level of chalk is getting set to zero - but that's a discussion for another day on another repo.

No I think it's due to our changes in AVA 3. Before, we kinda made it look like the worker process supported colors. We no longer do. Therefore, your Chalk thinks it doesn't.

And thanks 😄

ctjlewis commented 1 year ago

Assuming latest chalk:

const { Instance } = require('chalk')
const chalk = new Instance({level: 3})

Originally posted by @novemberborn in https://github.com/avajs/ava/issues/2367#issuecomment-574597756

This is now:

import { Chalk } from "chalk"
const chalk = new Chalk({ level: 3 });

Edit: This workaround doesn't work for me somehow.

image