mirror of
https://github.com/vitest-dev/vitest.git
synced 2025-12-08 18:26:03 +00:00
fix: indicator position of error message (#3855)
This commit is contained in:
parent
06ca0b6f66
commit
3e1e7a1fe4
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -1 +1,3 @@
|
||||
* text=auto eol=lf
|
||||
|
||||
test/reporters/fixtures/indicator-position.test.js eol=crlf
|
||||
|
||||
@ -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[] = []
|
||||
|
||||
@ -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)
|
||||
@ -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)))
|
||||
}
|
||||
|
||||
15
test/reporters/fixtures/indicator-position.test.js
Normal file
15
test/reporters/fixtures/indicator-position.test.js
Normal file
@ -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
test/reporters/tests/indicator-position.test.ts
Normal file
37
test/reporters/tests/indicator-position.test.ts
Normal file
@ -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]⎯
|
||||
|
||||
"
|
||||
`)
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user