From 6ae7eaa26ce8bac6ccc6ee03ed0a238a4ce54b6c Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 1 Apr 2024 17:46:11 +0900 Subject: [PATCH] fix(vitest): check unhighlighted code for code frame line limit (#5465) --- packages/vitest/src/node/error.ts | 5 +++-- test/reporters/fixtures/code-frame-line-limit.test.ts | 6 ++++++ test/reporters/tests/code-frame-line-limit.test.ts | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/reporters/fixtures/code-frame-line-limit.test.ts create mode 100644 test/reporters/tests/code-frame-line-limit.test.ts diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index d4e94fb7018f..d099bc482bb9 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -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' @@ -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)) diff --git a/test/reporters/fixtures/code-frame-line-limit.test.ts b/test/reporters/fixtures/code-frame-line-limit.test.ts new file mode 100644 index 000000000000..bda42b218136 --- /dev/null +++ b/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 }]) +}) diff --git a/test/reporters/tests/code-frame-line-limit.test.ts b/test/reporters/tests/code-frame-line-limit.test.ts new file mode 100644 index 000000000000..39fb25b31f53 --- /dev/null +++ b/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 },') +})