Skip to content

Commit

Permalink
fix(vitest): check unhighlighted code for code frame line limit (#5465)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 1, 2024
1 parent e4e939b commit 6ae7eaa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/vitest/src/node/error.ts
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
@@ -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
@@ -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 },')
})

0 comments on commit 6ae7eaa

Please sign in to comment.