Skip to content
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

The stackStr in test.result.errors has ANSI color codes #2868

Closed
6 tasks done
roerohan opened this issue Feb 14, 2023 · 2 comments
Closed
6 tasks done

The stackStr in test.result.errors has ANSI color codes #2868

roerohan opened this issue Feb 14, 2023 · 2 comments

Comments

@roerohan
Copy link

roerohan commented Feb 14, 2023

Describe the bug

When creating a custom reporter, the test.result.errors[index].stack and test.result.errors[index].stackStr consist of some ANSI color codes (possibly to show color when logged in the console). However, this affects all other custom reporters that do not want to log to the console, for instance, the one I shared in the link above.

Reproduction

TLDR;

Check out this PR.

Reproduction

When using the GithubReporter from GithubReporter.ts, when expect failed in a test with .not.toBeCalled(), there were ANSI characters in the stack trace, whereas when the expect failed in a test with expect(a).toBe(b) there were no ANSI color codes in the stack trace.

P.S. this is reproducible only on the sample-test branch of the repository, because there is a regex to filter out ANSI codes from the stack trace on the main branch.

import { defineConfig } from 'vitest/config';
// import { GithubReporter } from 'vitest-github-action';
import { GithubReporter } from './src';

export default defineConfig({
    test: {
        reporters: process.env.GITHUB_ACTIONS
            ? ['default', new GithubReporter()]
            : 'default',
    },
});

Test file that has ANSI colors in stack trace when there's an error:

import {beforeEach, describe, expect, test, vi} from 'vitest';

let sum: () => void;

describe('Given sum() exists', () => {
	beforeEach(() => {
		sum = vi.fn();
	});

	describe('When sum() is called', () => {
		beforeEach(() => {
			sum();
		});

		test('sum() should be called', () => {
			expect(sum).toBeCalled();
		});

		test('sum() should not be called', () => {
			expect(sum).not.toBeCalled();
		});
	});
});

Test file that does not have ANSI colors in the stack trace when there's an error:

import {beforeEach, describe, expect, test} from 'vitest';
import sum from './sum';

function sum(a: number, b: number) {
	return a + b;
}

let val: number;

describe('Given sum() was called', () => {
	describe('When the arguments are 1 and 1', () => {
		beforeEach(() => {
			val = sum(1, 1);
		});

		// Passing test
		test('Then the sum should be 2', () => {
			expect(val).toBe(2);
		});

		// Failing test
		test('Then the sum should be 3', () => {
			expect(val).toBe(3);
		});
	});
});

What is weird is that both of these actually show colors when these errors are logged in the console. I'm not sure if that's the vitest reporter, because seemingly, the colors (or colorful error messages, as in the image below) are not there in the stack trace logged in sum.test.ts.

image

System Info

System:
    OS: Linux 5.19 Manjaro Linux
    CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics
    Memory: 5.55 GB / 14.99 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 19.1.0 - ~/.nvm/versions/node/v19.1.0/bin/node
    npm: 6.14.15 - ~/Documents/Repos/dyte/web-core/node_modules/.bin/npm
  Browsers:
    Chrome: 108.0.5359.124
    Firefox: 109.0.1
  npmPackages:
    @vitest/coverage-c8: ^0.28.3 => 0.28.3 
    vite: 3.0.1 => 3.0.1 
    vitest: ^0.28.5 => 0.28.5

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

Seems like we need to add more checks for displaying colors. I removed picocolors in #2828 (which will probably be extracted into its own PR), and we can do that there.

@sheremet-va
Copy link
Member

Colors are disabled if GITHUB_ACTIONS is present

@github-actions github-actions bot locked and limited conversation to collaborators Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants