From 3e1e7a1fe40790ad87a36c4815287df8cc619234 Mon Sep 17 00:00:00 2001 From: Han Date: Thu, 3 Aug 2023 15:45:31 +0800 Subject: [PATCH] fix: indicator position of error message (#3855) --- .gitattributes | 2 + packages/vitest/src/node/error.ts | 5 ++- .../fixtures/indicator-position.test.js | 15 ++++++++ .../tests/indicator-position.test.ts | 37 +++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 test/reporters/fixtures/indicator-position.test.js create mode 100644 test/reporters/tests/indicator-position.test.ts diff --git a/.gitattributes b/.gitattributes index 6313b56c5784..e5f95a846e74 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,3 @@ * text=auto eol=lf + +test/reporters/fixtures/indicator-position.test.js eol=crlf diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index c09c8628dbf9..364b82ecb491 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -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))) } diff --git a/test/reporters/fixtures/indicator-position.test.js b/test/reporters/fixtures/indicator-position.test.js new file mode 100644 index 000000000000..21caeafdf1ac --- /dev/null +++ b/test/reporters/fixtures/indicator-position.test.js @@ -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) +}) diff --git a/test/reporters/tests/indicator-position.test.ts b/test/reporters/tests/indicator-position.test.ts new file mode 100644 index 000000000000..d3a4d8775232 --- /dev/null +++ b/test/reporters/tests/indicator-position.test.ts @@ -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]⎯ + + " + `) +})