Skip to content

fix(vitest): strip ansi code from console output in junit reporter - #5007

Closed
hi-ogawa wants to merge 4 commits into
vitest-dev:mainfrom
hi-ogawa:fix-junit-strip-ansi
Closed

hi-ogawa wants to merge 4 commits into
vitest-dev:mainfrom
hi-ogawa:fix-junit-strip-ansi

Conversation

@hi-ogawa

@hi-ogawa hi-ogawa commented Jan 19, 2024 •

Copy link
Copy Markdown
Collaborator

Description

I'm not sure the need of preserving color for this logging output, so I propose to always strip it for junit without configuration to toggle color. Please let me know if this makes sense.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify

netlify Bot commented Jan 19, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit 1cab176
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/65aa218c7f331e0008a41a66

@AriPerkkio

Copy link
Copy Markdown
Member

I remember adding this environment variable for reporters test case just because of this issue. Can we now remove it from here?

"test": "NO_COLOR=1 vitest run"

Comment on lines +50 to +52
const resultJson = parse(metaJson.replace(new RegExp(vitestRoot, 'g'), '<rootDir>'), (_key, value) => {
return typeof value === 'string' ? stripAnsi(value) : value
})

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NO_COLOR was also affecting html reporter, so I stripped it just for this test case.

@hi-ogawa
hi-ogawa marked this pull request as ready for review January 19, 2024 07:43
@sheremet-va

Copy link
Copy Markdown
Member

I am not sure why this is a problem. NO_COLOR disables this, right? What makes this reporter special that we always have to remove the colors?

@AriPerkkio

Copy link
Copy Markdown
Member

What makes this reporter special that we always have to remove the colors?

I think it makes sense for users to keep colors enabled for reporters that output on terminal on CI. But they would still like to generate other reports on file system and keep their content human-readable.

@hi-ogawa

hi-ogawa commented Jan 19, 2024 •

Copy link
Copy Markdown
Collaborator Author

I think it makes sense for users to keep colors enabled for reporters that output on terminal on CI. But they would still like to generate other reports on file system and keep their content human-readable.

I haven't properly followed up with the issue author #4958 but I assumed that's the intention and made sense to me as a default junit reporter behavior. But, disabling entirely by NO_COLOR should be not a big deal and I totally get that applying stripAnsi here feels somewhat too artificial.
(for example, users might be intentionally doing console.log("\x1B[33m hi \x1B[39m") in the user code and expect it to appear in junit xml)

@sheremet-va

Copy link
Copy Markdown
Member

I think it makes sense for users to keep colors enabled for reporters that output on terminal on CI. But they would still like to generate other reports on file system and keep their content human-readable.

It just looks too irreversible to me. As @hi-ogawa, what if the color is expected? I think we can add an option to toggle this. We can even disable colors by default if you think it is a better experience for most people.

@hi-ogawa

hi-ogawa commented Jan 19, 2024 •

Copy link
Copy Markdown
Collaborator Author

Thanks for the discussion. Yeah, I'll re-consider this change. Also, it's better to wait for the issue author's response to know the actual experience with current junit output and measure the significance of the issue. I will put this back to draft for now.

@hi-ogawa
hi-ogawa marked this pull request as draft January 19, 2024 10:33
@AriPerkkio

Copy link
Copy Markdown
Member

Another approach for this would be support for reporter options. I would imagine something like:

{
  test: {
    reporters: ["verbose", ["junit", { color: false }], "html"],
  },
}

This was mentioned here #3691 (comment) but no actions were taken. This would also benefit third party reporters.

@sheremet-va

Copy link
Copy Markdown
Member

Another approach for this would be support for reporter options

I think that would be nice to support but we didn't create an issue yet, true.

@hi-ogawa hi-ogawa mentioned this pull request Jan 25, 2024
4 tasks done
@hi-ogawa

Copy link
Copy Markdown
Collaborator Author

That sounds good to me. I created an issue here #5042

@AriPerkkio

Copy link
Copy Markdown
Member

Now that #5042 is finished, the JUnit reporter accepts some reporter options:

export interface JUnitOptions {
outputFile?: string
classname?: string
suiteName?: string
}

Maybe a new option should be introduced there, color?: boolean that defaults to true. Then user's could disable it like shown in #5007 (comment).

@hi-ogawa

Copy link
Copy Markdown
Collaborator Author

@AriPerkkio Thanks for the ping. For this junit issue, I'm becoming less convinced about this feature, so I'll close this for now. I think we can reconsider this if we get more reports.

@hi-ogawa hi-ogawa closed this Feb 12, 2024
@hi-ogawa
hi-ogawa deleted the fix-junit-strip-ansi branch February 12, 2024 03:12
@ssbarnea

Copy link
Copy Markdown

The original bug is still true and using environment variables is not possible as they have other side effects, like disabling coloring on vitest itself and worse, affecting the testing, as the tool being tested might react to the NO_COLOR, including node itself!

Sadly the original bug was closed and locked.

@github-actions github-actions Bot locked and limited conversation to collaborators Aug 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JUnit reporter sometimes adds ansi colors to the report

4 participants