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

fix(vitest): check unhighlighted code for code frame line limit #5465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import c from 'picocolors'
import cliTruncate from 'cli-truncate'
import type { StackTraceParserOptions } from '@vitest/utils/source-map'
import { inspect } from '@vitest/utils'
import stripAnsi from 'strip-ansi'
import type { ErrorWithDiff, ParsedStack } from '../types'
import { lineSplitRE, parseErrorStacktrace, positionToOffset } from '../utils/source-map'
import { F_POINTER } from '../utils/figures'
Expand Down Expand Up @@ -289,8 +290,8 @@ export function generateCodeFrame(

const lineLength = lines[j].length

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

res.push(lineNo(j + 1) + cliTruncate(lines[j].replace(/\t/g, ' '), columns - 5 - indent))
Expand Down
6 changes: 6 additions & 0 deletions test/reporters/fixtures/code-frame-line-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test, expect } from "vitest"

test("basic", () => {
// line length is 85 but highlight makes this line 245 chars
expect([{ prop: 7 }, { prop: 7 }, { prop: 7 }, { prop: 7 }]).toBe([{ another: 8 }])
})
9 changes: 9 additions & 0 deletions test/reporters/tests/code-frame-line-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'
import { resolve } from 'pathe'
import { runVitest } from '../../test-utils'

test('show code frame', async () => {
const filename = resolve('./fixtures/code-frame-line-limit.test.ts')
const { stderr } = await runVitest({ root: './fixtures' }, [filename])
expect(stderr).toContain('5| expect([{ prop: 7 },')
})