Skip to content

Commit

Permalink
fix: indicator position of error message (#3855)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenghan34 authored Aug 3, 2023
1 parent 06ca0b6 commit 3e1e7a1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
* text=auto eol=lf

test/reporters/fixtures/indicator-position.test.js eol=crlf
5 changes: 3 additions & 2 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export function generateCodeFrame(
const start = positionToOffset(source, lineNumber, columnNumber)
const end = start
const lines = source.split(lineSplitRE)
const nl = /\r\n/.test(source) ? 2 : 1
let count = 0
let res: string[] = []

Expand All @@ -261,7 +262,7 @@ export function generateCodeFrame(
}

for (let i = 0; i < lines.length; i++) {
count += lines[i].length + 1
count += lines[i].length + nl
if (count >= start) {
for (let j = i - range; j <= i + range || end > count; j++) {
if (j < 0 || j >= lines.length)
Expand All @@ -277,7 +278,7 @@ export function generateCodeFrame(

if (j === i) {
// push underline
const pad = start - (count - lineLength)
const pad = start - (count - lineLength) + (nl - 1)
const length = Math.max(1, end > count ? lineLength - pad : end - start)
res.push(lineNo() + ' '.repeat(pad) + c.red('^'.repeat(length)))
}
Expand Down
15 changes: 15 additions & 0 deletions test/reporters/fixtures/indicator-position.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable no-multiple-empty-lines */
// this file should be in CRLF format
import { expect, test } from 'vitest'







test('', async () => {
expect(1 + 1).toBe(3)
expect(1 + 1).toBe(2)
expect(2 + 2).toBe(4)
})
37 changes: 37 additions & 0 deletions test/reporters/tests/indicator-position.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { readFileSync } from 'node:fs'
import { expect, test } from 'vitest'
import { resolve } from 'pathe'
import { runVitest } from '../../test-utils'

test('should print correct indicator position', async () => {
const filename = resolve('./fixtures/indicator-position.test.js')
const { stderr } = await runVitest({ root: './fixtures' }, [filename])
const code = readFileSync(filename, 'utf-8')

expect(code).toMatch(/\r\n/)
expect(stderr).toBeTruthy()
expect(stderr).toMatchInlineSnapshot(`
"⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
FAIL indicator-position.test.js >
AssertionError: expected 2 to be 3 // Object.is equality
- Expected
+ Received
- 3
+ 2
❯ indicator-position.test.js:12:17
10|
11| test('', async () => {
12| expect(1 + 1).toBe(3)
| ^
13| expect(1 + 1).toBe(2)
14| expect(2 + 2).toBe(4)
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
"
`)
})

0 comments on commit 3e1e7a1

Please sign in to comment.