Skip to content

Commit

Permalink
refactor: update colors implementation (vitest-dev#2975)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 7, 2023
1 parent 0699182 commit 18d8019
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 99 deletions.
109 changes: 51 additions & 58 deletions packages/utils/src/colors.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
import { SAFE_COLORS_SYMBOL } from './constants'

type Colors = ReturnType<typeof createColors>
const colorsMap = {
bold: ['\x1B[1m', '\x1B[22m', '\x1B[22m\x1B[1m'],
dim: ['\x1B[2m', '\x1B[22m', '\x1B[22m\x1B[2m'],
italic: ['\x1B[3m', '\x1B[23m'],
underline: ['\x1B[4m', '\x1B[24m'],
inverse: ['\x1B[7m', '\x1B[27m'],
hidden: ['\x1B[8m', '\x1B[28m'],
strikethrough: ['\x1B[9m', '\x1B[29m'],
black: ['\x1B[30m', '\x1B[39m'],
red: ['\x1B[31m', '\x1B[39m'],
green: ['\x1B[32m', '\x1B[39m'],
yellow: ['\x1B[33m', '\x1B[39m'],
blue: ['\x1B[34m', '\x1B[39m'],
magenta: ['\x1B[35m', '\x1B[39m'],
cyan: ['\x1B[36m', '\x1B[39m'],
white: ['\x1B[37m', '\x1B[39m'],
gray: ['\x1B[90m', '\x1B[39m'],
bgBlack: ['\x1B[40m', '\x1B[49m'],
bgRed: ['\x1B[41m', '\x1B[49m'],
bgGreen: ['\x1B[42m', '\x1B[49m'],
bgYellow: ['\x1B[43m', '\x1B[49m'],
bgBlue: ['\x1B[44m', '\x1B[49m'],
bgMagenta: ['\x1B[45m', '\x1B[49m'],
bgCyan: ['\x1B[46m', '\x1B[49m'],
bgWhite: ['\x1B[47m', '\x1B[49m'],
} as const

const colors = [
'reset',
'bold',
'dim',
'italic',
'underline',
'inverse',
'hidden',
'strikethrough',
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'gray',
'bgBlack',
'bgRed',
'bgGreen',
'bgYellow',
'bgBlue',
'bgMagenta',
'bgCyan',
'bgWhite',
] as const
type ColorName = keyof typeof colorsMap
type ColorsMethods = {
[Key in ColorName]: (input: unknown) => string
}

type Colors = ColorsMethods & {
isColorSupported: boolean
reset: (input: unknown) => string
}

const colorsEntries = Object.entries(colorsMap)

const string = (str: unknown) => String(str)
string.open = ''
string.close = ''

const defaultColors = colors.reduce((acc, key) => {
acc[key] = string
const defaultColors = colorsEntries.reduce((acc, [key]) => {
acc[key as ColorName] = string
return acc
}, {} as Colors)
}, { isColorSupported: false } as Colors)

export function getDefaultColors(): Colors {
return { ...defaultColors }
Expand All @@ -47,7 +56,7 @@ export function getColors(): Colors {
return (globalThis as any)[SAFE_COLORS_SYMBOL] || defaultColors
}

export function createColors(isTTY = false) {
export function createColors(isTTY = false): Colors {
const enabled = typeof process !== 'undefined'
&& !('NO_COLOR' in process.env || process.argv.includes('--no-color'))
&& !('GITHUB_ACTIONS' in process.env)
Expand All @@ -66,7 +75,7 @@ export function createColors(isTTY = false) {

const formatter = (open: string, close: string, replace = open) => {
const fn = (input: unknown) => {
const string = `${input}`
const string = String(input)
const index = string.indexOf(close, open.length)
return ~index
? open + replaceClose(string, close, replace, index) + close
Expand All @@ -78,34 +87,18 @@ export function createColors(isTTY = false) {
}

// based on "https://github.com/alexeyraspopov/picocolors", but browser-friendly
return {
const colorsObject = {
isColorSupported: enabled,
reset: enabled ? (s: string) => `\x1B[0m${s}\x1B[0m` : string,
bold: enabled ? formatter('\x1B[1m', '\x1B[22m', '\x1B[22m\x1B[1m') : string,
dim: enabled ? formatter('\x1B[2m', '\x1B[22m', '\x1B[22m\x1B[2m') : string,
italic: enabled ? formatter('\x1B[3m', '\x1B[23m') : string,
underline: enabled ? formatter('\x1B[4m', '\x1B[24m') : string,
inverse: enabled ? formatter('\x1B[7m', '\x1B[27m') : string,
hidden: enabled ? formatter('\x1B[8m', '\x1B[28m') : string,
strikethrough: enabled ? formatter('\x1B[9m', '\x1B[29m') : string,
black: enabled ? formatter('\x1B[30m', '\x1B[39m') : string,
red: enabled ? formatter('\x1B[31m', '\x1B[39m') : string,
green: enabled ? formatter('\x1B[32m', '\x1B[39m') : string,
yellow: enabled ? formatter('\x1B[33m', '\x1B[39m') : string,
blue: enabled ? formatter('\x1B[34m', '\x1B[39m') : string,
magenta: enabled ? formatter('\x1B[35m', '\x1B[39m') : string,
cyan: enabled ? formatter('\x1B[36m', '\x1B[39m') : string,
white: enabled ? formatter('\x1B[37m', '\x1B[39m') : string,
gray: enabled ? formatter('\x1B[90m', '\x1B[39m') : string,
bgBlack: enabled ? formatter('\x1B[40m', '\x1B[49m') : string,
bgRed: enabled ? formatter('\x1B[41m', '\x1B[49m') : string,
bgGreen: enabled ? formatter('\x1B[42m', '\x1B[49m') : string,
bgYellow: enabled ? formatter('\x1B[43m', '\x1B[49m') : string,
bgBlue: enabled ? formatter('\x1B[44m', '\x1B[49m') : string,
bgMagenta: enabled ? formatter('\x1B[45m', '\x1B[49m') : string,
bgCyan: enabled ? formatter('\x1B[46m', '\x1B[49m') : string,
bgWhite: enabled ? formatter('\x1B[47m', '\x1B[49m') : string,
} as Colors

for (const [name, formatterArgs] of colorsEntries) {
colorsObject[name as ColorName] = enabled
? formatter(...formatterArgs as [string, string])
: string
}

return colorsObject
}

export function setupColors(colors: Colors) {
Expand Down
80 changes: 40 additions & 40 deletions test/core/test/__snapshots__/mocked.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`mocked function which fails on toReturnWith > just one call 1`] = `
"expected \\"spy\\" to return with: 2 at least once[90m
"expected \\"spy\\" to return with: 2 at least once
Received:
[1m 1st spy call return:
1st spy call return:
 1
2

1
2
Number of calls: 1
"
Number of calls: 1
"
`;

exports[`mocked function which fails on toReturnWith > multi calls 1`] = `
"expected \\"spy\\" to return with: 2 at least once[90m
"expected \\"spy\\" to return with: 2 at least once
Received:
 1st spy call return:
1st spy call return:
1
2
 1
2
2nd spy call return:
 2nd spy call return:
1
2
 1
2
3rd spy call return:
 3rd spy call return:
1
2
 1
2

Number of calls: [1m3[22m
[39m"
Number of calls: 3
"
`;

exports[`mocked function which fails on toReturnWith > oject type 1`] = `
"expected \\"spy\\" to return with: { a: '4' } at least once[90m
"expected \\"spy\\" to return with: { a: '4' } at least once
Received:
[1m 1st spy call return:
1st spy call return:
[22m Object {
[32m- \\"a\\": \\"1\\",[90m
[31m+ \\"a\\": \\"4\\",[90m
Object {
- \\"a\\": \\"1\\",
+ \\"a\\": \\"4\\",
}
[1m 2nd spy call return:
2nd spy call return:
[22m Object {
[32m- \\"a\\": \\"1\\",[90m
[31m+ \\"a\\": \\"4\\",[90m
Object {
- \\"a\\": \\"1\\",
+ \\"a\\": \\"4\\",
}
[1m 3rd spy call return:
3rd spy call return:
[22m Object {
[32m- \\"a\\": \\"1\\",[90m
[31m+ \\"a\\": \\"4\\",[90m
Object {
- \\"a\\": \\"1\\",
+ \\"a\\": \\"4\\",
}

Number of calls: 3
"
Number of calls: 3
"
`;

exports[`mocked function which fails on toReturnWith > zero call 1`] = `
"expected \\"spy\\" to return with: 2 at least once[90m
"expected \\"spy\\" to return with: 2 at least once
Received:

Number of calls: 0
"
Number of calls: 0
"
`;
3 changes: 2 additions & 1 deletion test/core/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getDefaultColors, setupColors } from '@vitest/utils'
import { beforeEach, vi } from 'vitest'

vi.mock('../src/global-mock', () => ({ mocked: true }))

beforeEach(() => {
// console.log(`hi ${s.name}`)
setupColors(getDefaultColors())
})

0 comments on commit 18d8019

Please sign in to comment.