From 288cebddfa356e42c2a3ab40e747ffb5d424a1ab Mon Sep 17 00:00:00 2001 From: Han Feng Date: Tue, 1 Aug 2023 18:28:04 +0800 Subject: [PATCH 1/4] fix: indicator position --- packages/vitest/src/node/error.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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))) } From d90f338ef1b21e3c77cdff78b8716cd627a8e7ee Mon Sep 17 00:00:00 2001 From: Han Feng Date: Tue, 1 Aug 2023 22:20:59 +0800 Subject: [PATCH 2/4] test: add test --- .gitattributes | 2 + .../fixtures/indicator-position.test.ts | 15 ++++++++ .../tests/indicator-position.test.ts | 37 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 test/reporters/fixtures/indicator-position.test.ts create mode 100644 test/reporters/tests/indicator-position.test.ts diff --git a/.gitattributes b/.gitattributes index 6313b56c5784..9e2126456063 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,3 @@ * text=auto eol=lf + +test/reporters/fixture/indicator-position.test.ts eol=crlf diff --git a/test/reporters/fixtures/indicator-position.test.ts b/test/reporters/fixtures/indicator-position.test.ts new file mode 100644 index 000000000000..21caeafdf1ac --- /dev/null +++ b/test/reporters/fixtures/indicator-position.test.ts @@ -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..dea3476b70df --- /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.ts') + 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.ts > + AssertionError: expected 2 to be 3 // Object.is equality + + - Expected + + Received + + - 3 + + 2 + + ❯ indicator-position.test.ts: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]⎯ + + " + `) +}) From 922eefb9609e43af3e10e7f2a58ca50d2c743b10 Mon Sep 17 00:00:00 2001 From: Han Feng Date: Wed, 2 Aug 2023 09:56:00 +0800 Subject: [PATCH 3/4] fix: typo --- .gitattributes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index 9e2126456063..baf5ce686376 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,3 @@ * text=auto eol=lf -test/reporters/fixture/indicator-position.test.ts eol=crlf +test/reporters/fixtures/indicator-position.test.ts eol=crlf From 51d6c467bb942251c463f610e2b5c014021ad580 Mon Sep 17 00:00:00 2001 From: Han Feng Date: Wed, 2 Aug 2023 18:57:27 +0800 Subject: [PATCH 4/4] chore: convert to js file --- .gitattributes | 2 +- ...ndicator-position.test.ts => indicator-position.test.js} | 0 test/reporters/tests/indicator-position.test.ts | 6 +++--- 3 files changed, 4 insertions(+), 4 deletions(-) rename test/reporters/fixtures/{indicator-position.test.ts => indicator-position.test.js} (100%) diff --git a/.gitattributes b/.gitattributes index baf5ce686376..e5f95a846e74 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,3 @@ * text=auto eol=lf -test/reporters/fixtures/indicator-position.test.ts eol=crlf +test/reporters/fixtures/indicator-position.test.js eol=crlf diff --git a/test/reporters/fixtures/indicator-position.test.ts b/test/reporters/fixtures/indicator-position.test.js similarity index 100% rename from test/reporters/fixtures/indicator-position.test.ts rename to test/reporters/fixtures/indicator-position.test.js diff --git a/test/reporters/tests/indicator-position.test.ts b/test/reporters/tests/indicator-position.test.ts index dea3476b70df..d3a4d8775232 100644 --- a/test/reporters/tests/indicator-position.test.ts +++ b/test/reporters/tests/indicator-position.test.ts @@ -4,7 +4,7 @@ import { resolve } from 'pathe' import { runVitest } from '../../test-utils' test('should print correct indicator position', async () => { - const filename = resolve('./fixtures/indicator-position.test.ts') + const filename = resolve('./fixtures/indicator-position.test.js') const { stderr } = await runVitest({ root: './fixtures' }, [filename]) const code = readFileSync(filename, 'utf-8') @@ -13,7 +13,7 @@ test('should print correct indicator position', async () => { expect(stderr).toMatchInlineSnapshot(` "⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯ - FAIL indicator-position.test.ts > + FAIL indicator-position.test.js > AssertionError: expected 2 to be 3 // Object.is equality - Expected @@ -22,7 +22,7 @@ test('should print correct indicator position', async () => { - 3 + 2 - ❯ indicator-position.test.ts:12:17 + ❯ indicator-position.test.js:12:17 10| 11| test('', async () => { 12| expect(1 + 1).toBe(3)