Skip to content

Commit 3e1e7a1

Browse files
authored
fix: indicator position of error message (#3855)
1 parent 06ca0b6 commit 3e1e7a1

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
* text=auto eol=lf
2+
3+
test/reporters/fixtures/indicator-position.test.js eol=crlf

packages/vitest/src/node/error.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export function generateCodeFrame(
251251
const start = positionToOffset(source, lineNumber, columnNumber)
252252
const end = start
253253
const lines = source.split(lineSplitRE)
254+
const nl = /\r\n/.test(source) ? 2 : 1
254255
let count = 0
255256
let res: string[] = []
256257

@@ -261,7 +262,7 @@ export function generateCodeFrame(
261262
}
262263

263264
for (let i = 0; i < lines.length; i++) {
264-
count += lines[i].length + 1
265+
count += lines[i].length + nl
265266
if (count >= start) {
266267
for (let j = i - range; j <= i + range || end > count; j++) {
267268
if (j < 0 || j >= lines.length)
@@ -277,7 +278,7 @@ export function generateCodeFrame(
277278

278279
if (j === i) {
279280
// push underline
280-
const pad = start - (count - lineLength)
281+
const pad = start - (count - lineLength) + (nl - 1)
281282
const length = Math.max(1, end > count ? lineLength - pad : end - start)
282283
res.push(lineNo() + ' '.repeat(pad) + c.red('^'.repeat(length)))
283284
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable no-multiple-empty-lines */
2+
// this file should be in CRLF format
3+
import { expect, test } from 'vitest'
4+
5+
6+
7+
8+
9+
10+
11+
test('', async () => {
12+
expect(1 + 1).toBe(3)
13+
expect(1 + 1).toBe(2)
14+
expect(2 + 2).toBe(4)
15+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { readFileSync } from 'node:fs'
2+
import { expect, test } from 'vitest'
3+
import { resolve } from 'pathe'
4+
import { runVitest } from '../../test-utils'
5+
6+
test('should print correct indicator position', async () => {
7+
const filename = resolve('./fixtures/indicator-position.test.js')
8+
const { stderr } = await runVitest({ root: './fixtures' }, [filename])
9+
const code = readFileSync(filename, 'utf-8')
10+
11+
expect(code).toMatch(/\r\n/)
12+
expect(stderr).toBeTruthy()
13+
expect(stderr).toMatchInlineSnapshot(`
14+
"⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
15+
16+
FAIL indicator-position.test.js >
17+
AssertionError: expected 2 to be 3 // Object.is equality
18+
19+
- Expected
20+
+ Received
21+
22+
- 3
23+
+ 2
24+
25+
❯ indicator-position.test.js:12:17
26+
10|
27+
11| test('', async () => {
28+
12| expect(1 + 1).toBe(3)
29+
| ^
30+
13| expect(1 + 1).toBe(2)
31+
14| expect(2 + 2).toBe(4)
32+
33+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
34+
35+
"
36+
`)
37+
})

0 commit comments

Comments
 (0)