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

Custom snapshot console colors (A11Y) #2401

Closed
4 tasks done
TheJaredWilcurt opened this issue Nov 30, 2022 · 3 comments · Fixed by #4063
Closed
4 tasks done

Custom snapshot console colors (A11Y) #2401

TheJaredWilcurt opened this issue Nov 30, 2022 · 3 comments · Fixed by #4063
Labels
enhancement New feature or request pr welcome

Comments

@TheJaredWilcurt
Copy link
Contributor

Clear and concise description of the problem

  • Colors work differently in different editors/consoles/terminals/shells/CI's/etc.
  • Some people have different visual, or cognitive impairments that make certain color combinations frustrating or difficult to discern.

Suggested solution

The default red/green approach is fine by me, and probably good for the majority of users. But it would be nice if there was a vite.config.js option for customizing the colors used by Vitest, particularly in snapshot failures.

import chalk from 'chalk';
import { defineConfig } from 'vite';

export default defineConfig({
  test: {
    diff: {
      // I'm not sure what the API would look like here, So I'll just use what
      // jest-diff documents for this example. The following is copied directly from
      // https://github.com/facebook/jest/tree/main/packages/jest-diff#options
      aAnnotation: 'Expected',
      aColor: chalk.green,
      aIndicator: '-',
      bAnnotation: 'Received',
      bColor: chalk.red,
      bIndicator: '+',
      changeColor: chalk.inverse,
      changeLineTrailingSpaceColor: string => string,
      commonColor: chalk.dim,
      commonIndicator: ' ',
      commonLineTrailingSpaceColor: string => string,
      compareKeys: undefined,
      contextLines: 5,
      emptyFirstOrLastLinePlaceholder: '',
      expand: true,
      includeChangeCounts: false,
      omitAnnotationLines: false,
      patchColor: chalk.yellow
    }
  }
});

By exposing all options, it empowers communities to create their own commonly recommended settings, like so:

import { defineConfig } from 'vite';
import diff from 'vitest-deuteranopia-diffs';

export default defineConfig({ test: { diff } });
import { defineConfig } from 'vite';
import diff from 'vitest-cognitive-diffs';

export default defineConfig({ test: { diff } });
import { defineConfig } from 'vite';
import diff from 'vitest-win-cmd-diffs';

export default defineConfig({ test: { diff } });

etc.
There could even be an awesome-vitest-diffs repo with screenshots of each library, so users can more easily choose what works best for them or their teams. This could all be community-lead, but we need Vitest to allow for this.

Alternative

No response

Additional context

This same feature request was submitted to Jest, and approved for PR.

Validations

@mst101
Copy link

mst101 commented May 15, 2023

Yes, I'd really appreciate this. I find it hard to read red text on a black background and would love to be able to use a slightly different colour.

@Fitzpaa
Copy link

Fitzpaa commented Aug 18, 2023

Yes please!

@sheremet-va sheremet-va added enhancement New feature or request pr welcome labels Aug 23, 2023
@sheremet-va
Copy link
Member

sheremet-va commented Aug 23, 2023

We now use the same diff method that jest uses, so it should be possible to reuse the same config. PRs to implement this are welcome. Please, note that the new option should be a string that references a module with a default export containing this config.

It should be loaded inside a test context and passed down to the diff method. Also note that Vitest doesn't use chalk, but we use a compatible interface.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request pr welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants