Skip to content

Commit 68c5a82

Browse files
authored
test(cli): use stripVTControlCharacters before matching strings (vitejs#20493)
1 parent acd2a13 commit 68c5a82

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

playground/cli-module/__tests__/serve.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

4+
import { stripVTControlCharacters } from 'node:util'
45
import { execaCommand } from 'execa'
56
import kill from 'kill-port'
67
import {
@@ -117,14 +118,12 @@ async function startedOnPort(serverProcess, port, timeout) {
117118
let checkPort
118119
const startedPromise = new Promise<void>((resolve, reject) => {
119120
checkPort = (data) => {
120-
const str = data.toString()
121-
// hack, console output may contain color code gibberish
122-
// skip gibberish between localhost: and port number
121+
const str = stripVTControlCharacters(data.toString())
123122
const match = str.match(
124-
/(http:\/\/(?:localhost|127\.0\.0\.1|\[::1\]):).*(\d{4})/,
123+
/http:\/\/(?:localhost|127\.0\.0\.1|\[::1\]):(\d{4})/,
125124
)
126125
if (match) {
127-
const startedPort = parseInt(match[2], 10)
126+
const startedPort = parseInt(match[1], 10)
128127
if (startedPort === port) {
129128
resolve()
130129
} else {

playground/cli/__tests__/serve.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// this is automatically detected by playground/vitestSetup.ts and will replace
22
// the default e2e test serve behavior
33

4+
import { stripVTControlCharacters } from 'node:util'
45
import { execaCommand } from 'execa'
56
import kill from 'kill-port'
67
import {
@@ -120,14 +121,12 @@ async function startedOnPort(serverProcess, port, timeout) {
120121
let checkPort
121122
const startedPromise = new Promise<void>((resolve, reject) => {
122123
checkPort = (data) => {
123-
const str = data.toString()
124-
// hack, console output may contain color code gibberish
125-
// skip gibberish between localhost: and port number
124+
const str = stripVTControlCharacters(data.toString())
126125
const match = str.match(
127-
/(http:\/\/(?:localhost|127\.0\.0\.1|\[::1\]):).*(\d{4})/,
126+
/http:\/\/(?:localhost|127\.0\.0\.1|\[::1\]):(\d{4})/,
128127
)
129128
if (match) {
130-
const startedPort = parseInt(match[2], 10)
129+
const startedPort = parseInt(match[1], 10)
131130
if (startedPort === port) {
132131
resolve()
133132
} else {

0 commit comments

Comments
 (0)