Skip to content

Commit 5f75790

Browse files
authored
refactor: replace strip-ansi with stripVTControlCharacters from node:util (#6608)
1 parent 9edfa19 commit 5f75790

26 files changed

+72
-96
lines changed

packages/vitest/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@
200200
"micromatch": "^4.0.7",
201201
"pretty-format": "^29.7.0",
202202
"prompts": "^2.4.2",
203-
"strip-ansi": "^7.1.0",
204203
"strip-literal": "^2.1.0",
205204
"tinyglobby": "^0.2.6",
206205
"ws": "^8.18.0"

packages/vitest/src/node/error.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable prefer-template */
22
import { existsSync, readFileSync } from 'node:fs'
33
import { Writable } from 'node:stream'
4+
import { stripVTControlCharacters } from 'node:util'
45
import { normalize, relative } from 'pathe'
56
import c from 'tinyrainbow'
67
import cliTruncate from 'cli-truncate'
78
import type { ErrorWithDiff, ParsedStack } from '@vitest/utils'
89
import { inspect } from '@vitest/utils'
9-
import stripAnsi from 'strip-ansi'
1010
import {
1111
lineSplitRE,
1212
positionToOffset,
@@ -398,7 +398,7 @@ export function generateCodeFrame(
398398
const lineLength = lines[j].length
399399

400400
// too long, maybe it's a minified file, skip for codeframe
401-
if (stripAnsi(lines[j]).length > 200) {
401+
if (stripVTControlCharacters(lines[j]).length > 200) {
402402
return ''
403403
}
404404

packages/vitest/src/node/reporters/benchmark/table/tableRender.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import c from 'tinyrainbow'
23
import cliTruncate from 'cli-truncate'
3-
import stripAnsi from 'strip-ansi'
44
import type { Task } from '@vitest/runner'
55
import { getTests, notNullish } from '../../../../utils'
66
import { F_RIGHT } from '../../../../utils/figures'
@@ -75,7 +75,7 @@ function renderBenchmarkItems(result: BenchmarkResult) {
7575
function computeColumnWidths(results: BenchmarkResult[]): number[] {
7676
const rows = [tableHead, ...results.map(v => renderBenchmarkItems(v))]
7777
return Array.from(tableHead, (_, i) =>
78-
Math.max(...rows.map(row => stripAnsi(row[i]).length)))
78+
Math.max(...rows.map(row => stripVTControlCharacters(row[i]).length)))
7979
}
8080

8181
function padRow(row: string[], widths: number[]) {
@@ -215,7 +215,7 @@ export function renderTree(
215215
if (task.result?.state !== 'pass' && outputMap.get(task) != null) {
216216
let data: string | undefined = outputMap.get(task)
217217
if (typeof data === 'string') {
218-
data = stripAnsi(data.trim().split('\n').filter(Boolean).pop()!)
218+
data = stripVTControlCharacters(data.trim().split('\n').filter(Boolean).pop()!)
219219
if (data === '') {
220220
data = undefined
221221
}

packages/vitest/src/node/reporters/github-actions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import { getTasks } from '@vitest/runner/utils'
2-
import stripAnsi from 'strip-ansi'
33
import type { File } from '@vitest/runner'
44
import { getFullName } from '../../utils'
55
import { capturePrintError } from '../error'
@@ -64,7 +64,7 @@ export class GithubActionsReporter implements Reporter {
6464
line: String(stack.line),
6565
column: String(stack.column),
6666
},
67-
message: stripAnsi(result.output),
67+
message: stripVTControlCharacters(result.output),
6868
})
6969
this.ctx.logger.log(`\n${formatted}`)
7070
}

packages/vitest/src/node/reporters/junit.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { existsSync, promises as fs } from 'node:fs'
22
import { hostname } from 'node:os'
3+
import { stripVTControlCharacters } from 'node:util'
34
import { dirname, relative, resolve } from 'pathe'
45

56
import type { Task } from '@vitest/runner'
67
import { getSuites } from '@vitest/runner/utils'
7-
import stripAnsi from 'strip-ansi'
88
import type { Vitest } from '../core'
99
import type { Reporter } from '../types/reporter'
1010
import { getOutputFile } from '../../utils/config-helpers'
@@ -233,7 +233,7 @@ export class JUnitReporter implements Reporter {
233233
{ project: this.ctx.getProjectByTaskId(task.id), task },
234234
)
235235
await this.baseLog(
236-
escapeXML(stripAnsi(result.output.trim())),
236+
escapeXML(stripVTControlCharacters(result.output.trim())),
237237
)
238238
},
239239
)

packages/vitest/src/node/reporters/renderers/listRenderer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import c from 'tinyrainbow'
23
import cliTruncate from 'cli-truncate'
3-
import stripAnsi from 'strip-ansi'
44
import type { SuiteHooks, Task } from '@vitest/runner'
55
import { getTests, notNullish } from '../../../utils'
66
import { F_RIGHT } from '../../../utils/figures'
@@ -183,7 +183,7 @@ function renderTree(
183183
if (task.result?.state !== 'pass' && outputMap.get(task) != null) {
184184
let data: string | undefined = outputMap.get(task)
185185
if (typeof data === 'string') {
186-
data = stripAnsi(data.trim().split('\n').filter(Boolean).pop()!)
186+
data = stripVTControlCharacters(data.trim().split('\n').filter(Boolean).pop()!)
187187
if (data === '') {
188188
data = undefined
189189
}

packages/vitest/src/node/reporters/renderers/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import { basename, dirname, isAbsolute, relative } from 'pathe'
23
import c from 'tinyrainbow'
3-
import stripAnsi from 'strip-ansi'
44
import type { SuiteHooks, Task } from '@vitest/runner'
55
import type { SnapshotSummary } from '@vitest/snapshot'
66
import { slash } from '../../../utils/base'
@@ -37,7 +37,7 @@ export function divider(text?: string, left?: number, right?: number) {
3737
const cols = getCols()
3838

3939
if (text) {
40-
const textLength = stripAnsi(text).length
40+
const textLength = stripVTControlCharacters(text).length
4141
if (left == null && right != null) {
4242
left = cols - textLength - right
4343
}

packages/vitest/src/node/watch-filter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import readline from 'node:readline'
22
import type { Writable } from 'node:stream'
3+
import { stripVTControlCharacters } from 'node:util'
34
import c from 'tinyrainbow'
4-
import stripAnsi from 'strip-ansi'
55
import { createDefer } from '@vitest/utils'
66
import { stdout as getStdout } from '../utils'
77

@@ -198,7 +198,7 @@ export class WatchFilter {
198198
const columns = 'columns' in this.stdout ? this.stdout.columns : 80
199199

200200
// We have to take care of screen width in case of long lines
201-
rows += 1 + Math.floor(Math.max(stripAnsi(line).length - 1, 0) / columns)
201+
rows += 1 + Math.floor(Math.max(stripVTControlCharacters(line).length - 1, 0) / columns)
202202
}
203203

204204
this.write(`${ESC}1G`) // move to the beginning of the line

pnpm-lock.yaml

-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browser/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"playwright": "^1.41.0",
2929
"react": "^18.3.1",
3030
"react-dom": "^18.3.1",
31-
"strip-ansi": "^7.1.0",
3231
"url": "^0.11.3",
3332
"vitest": "workspace:*",
3433
"vitest-browser-react": "^0.0.1",

test/browser/test/commands.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ declare module '@vitest/browser/context' {
5858
arg1: string
5959
arg2: string
6060
}>
61+
62+
stripVTControlCharacters: (text: string) => Promise<string>
6163
}
6264
}

test/browser/test/utils.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inspect } from 'vitest/utils'
22
import { afterEach, expect, it, test } from 'vitest'
3-
import stripAnsi from 'strip-ansi'
3+
import { commands } from '@vitest/browser/context'
44

55
import { prettyDOM } from '@vitest/browser/utils'
66

@@ -12,28 +12,28 @@ it('utils package correctly uses loupe', async () => {
1212
expect(inspect({ test: 1 })).toBe('{ test: 1 }')
1313
})
1414

15-
test('prints default document', () => {
16-
expect(stripAnsi(prettyDOM())).toMatchSnapshot()
15+
test('prints default document', async () => {
16+
expect(await commands.stripVTControlCharacters(prettyDOM())).toMatchSnapshot()
1717

1818
const div = document.createElement('div')
1919
div.innerHTML = '<span>hello</span>'
2020
document.body.append(div)
2121

22-
expect(stripAnsi(prettyDOM())).toMatchSnapshot()
22+
expect(await commands.stripVTControlCharacters(prettyDOM())).toMatchSnapshot()
2323
})
2424

25-
test('prints the element', () => {
25+
test('prints the element', async () => {
2626
const div = document.createElement('div')
2727
div.innerHTML = '<span>hello</span>'
2828
document.body.append(div)
2929

30-
expect(stripAnsi(prettyDOM())).toMatchSnapshot()
30+
expect(await commands.stripVTControlCharacters(prettyDOM())).toMatchSnapshot()
3131
})
3232

33-
test('prints the element with attributes', () => {
33+
test('prints the element with attributes', async () => {
3434
const div = document.createElement('div')
3535
div.innerHTML = '<span class="some-name" data-test-id="33" id="5">hello</span>'
3636
document.body.append(div)
3737

38-
expect(stripAnsi(prettyDOM())).toMatchSnapshot()
38+
expect(await commands.stripVTControlCharacters(prettyDOM())).toMatchSnapshot()
3939
})

test/browser/vitest.config.mts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { dirname, resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
3+
import * as util from 'node:util'
34
import { defineConfig } from 'vitest/config'
45
import type { BrowserCommand } from 'vitest/node'
56

@@ -14,6 +15,10 @@ const myCustomCommand: BrowserCommand<[arg1: string, arg2: string]> = ({ testPat
1415
return { testPath, arg1, arg2 }
1516
}
1617

18+
const stripVTControlCharacters: BrowserCommand<[text: string]> = (_, text) => {
19+
return util.stripVTControlCharacters(text)
20+
}
21+
1722
export default defineConfig({
1823
server: {
1924
headers: {
@@ -70,6 +75,7 @@ export default defineConfig({
7075
],
7176
commands: {
7277
myCustomCommand,
78+
stripVTControlCharacters,
7379
},
7480
},
7581
alias: {

test/core/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"debug": "^4.3.4",
2727
"immutable": "5.0.0-beta.5",
2828
"memfs": "^4.8.2",
29-
"strip-ansi": "^7.1.0",
3029
"sweetalert2": "^11.6.16",
3130
"tinyrainbow": "^1.2.0",
3231
"tinyspy": "^1.0.2",

0 commit comments

Comments
 (0)