Skip to content

Commit 6ae7eaa

Browse files
authoredApr 1, 2024··
fix(vitest): check unhighlighted code for code frame line limit (#5465)
1 parent e4e939b commit 6ae7eaa

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed
 

‎packages/vitest/src/node/error.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import c from 'picocolors'
55
import cliTruncate from 'cli-truncate'
66
import type { StackTraceParserOptions } from '@vitest/utils/source-map'
77
import { inspect } from '@vitest/utils'
8+
import stripAnsi from 'strip-ansi'
89
import type { ErrorWithDiff, ParsedStack } from '../types'
910
import { lineSplitRE, parseErrorStacktrace, positionToOffset } from '../utils/source-map'
1011
import { F_POINTER } from '../utils/figures'
@@ -289,8 +290,8 @@ export function generateCodeFrame(
289290

290291
const lineLength = lines[j].length
291292

292-
// to long, maybe it's a minified file, skip for codeframe
293-
if (lineLength > 200)
293+
// too long, maybe it's a minified file, skip for codeframe
294+
if (stripAnsi(lines[j]).length > 200)
294295
return ''
295296

296297
res.push(lineNo(j + 1) + cliTruncate(lines[j].replace(/\t/g, ' '), columns - 5 - indent))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test, expect } from "vitest"
2+
3+
test("basic", () => {
4+
// line length is 85 but highlight makes this line 245 chars
5+
expect([{ prop: 7 }, { prop: 7 }, { prop: 7 }, { prop: 7 }]).toBe([{ another: 8 }])
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, test } from 'vitest'
2+
import { resolve } from 'pathe'
3+
import { runVitest } from '../../test-utils'
4+
5+
test('show code frame', async () => {
6+
const filename = resolve('./fixtures/code-frame-line-limit.test.ts')
7+
const { stderr } = await runVitest({ root: './fixtures' }, [filename])
8+
expect(stderr).toContain('5| expect([{ prop: 7 },')
9+
})

0 commit comments

Comments
 (0)
Please sign in to comment.