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: indicator position of error message #3855

Merged
merged 7 commits into from
Aug 3, 2023
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
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]⎯

"
`)
})