Skip to content

Commit

Permalink
feat: show error on the line it failed in watch mode #37
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed May 23, 2022
1 parent 8a4aa10 commit 793e2fc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"@types/glob": "^7.2.0",
"@types/node": "14.x",
"@types/semver": "^7.3.9",
"@types/stack-utils": "^2.0.1",
"@types/vscode": "^1.63.0",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.12.1",
Expand All @@ -161,6 +162,7 @@
"mocha": "^9.2.1",
"prettier": "^2.5.1",
"semver": "^7.3.5",
"stack-utils": "^2.0.5",
"tsup": "^5.12.7",
"typescript": "^4.5.5",
"vite": "^2.8.6",
Expand Down
61 changes: 45 additions & 16 deletions src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { ChildProcess } from 'child_process'
import path from 'path'
import getPort from 'get-port'
import { getTasks } from '@vitest/ws-client'
import { effect, ref } from '@vue/reactivity'
import Fuse from 'fuse.js'
import type { File, Task } from 'vitest'
import StackUtils from 'stack-utils'
import type { ErrorWithDiff, File, Task } from 'vitest'
import type { TestController, TestItem, TestRun } from 'vscode'
import { Disposable, TestMessage, TestRunRequest, workspace } from 'vscode'
import { Disposable, Location, Position, TestMessage, TestRunRequest, Uri, workspace } from 'vscode'
import { Lock } from 'mighty-promise'
import { getConfig } from './config'
import type { TestFileDiscoverer } from './discover'
Expand All @@ -14,6 +16,10 @@ import { buildWatchClient } from './pure/watch/client'
import type { TestFile } from './TestData'
import { TestCase, TestDescribe, WEAKMAP_TEST_DATA } from './TestData'

const stackUtils = new StackUtils({
cwd: '/ensure_absolute_paths',
})
export interface DebuggerLocation { path: string; line: number; column: number }
export class TestWatcher extends Disposable {
static cache: undefined | TestWatcher
static isWatching() {
Expand Down Expand Up @@ -283,20 +289,11 @@ export class TestWatcher extends Disposable {
run.passed(data.item, task.result.duration)
break
case 'fail':
if (task.result.error?.actual != null && task.result.error?.expected != null) {
run.failed(
data.item,
TestMessage.diff(task.result.error?.message ?? '', task.result?.error.expected, task.result?.error.actual),
task.result.duration,
)
}
else {
run.failed(
data.item,
new TestMessage(task.result.error?.message ?? ''),
task.result.duration,
)
}
run.failed(
data.item,
testMessageForTestError(data.item, task.result.error),
task.result.duration,
)
break
case 'skip':
case 'todo':
Expand Down Expand Up @@ -368,3 +365,35 @@ export class TestWatcher extends Disposable {
}
}
}

function parseLocationFromStack(testItem: TestItem, stack: string | undefined): DebuggerLocation | undefined {
const lines = stack?.split('\n') || []
for (const line of lines) {
const frame = stackUtils.parseLine(line)
if (!frame || !frame.file || !frame.line || !frame.column)
continue
frame.file = frame.file.replace(/\//g, path.sep)
if (testItem.uri!.fsPath === frame.file) {
return {
path: frame.file,
line: frame.line,
column: frame.column,
}
}
}
}

function testMessageForTestError(testItem: TestItem, error: ErrorWithDiff | undefined): TestMessage {
let testMessage
if (error?.actual != null && error?.expected != null)
testMessage = TestMessage.diff(error?.message ?? '', error.expected, error.actual)
else
testMessage = new TestMessage(error?.message ?? '')

const location = parseLocationFromStack(testItem, error?.stack)
if (location) {
const position = new Position(location.line - 1, location.column - 1)
testMessage.location = new Location(Uri.file(location.path), position)
}
return testMessage
}
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@
resolved "https://registry.npmmirror.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc"
integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ==

"@types/stack-utils@^2.0.1":
version "2.0.1"
resolved "https://registry.npmmirror.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==

"@types/unist@*", "@types/unist@^2.0.2":
version "2.0.6"
resolved "https://registry.npmmirror.com/@types/unist/-/unist-2.0.6.tgz#250a7b16c3b91f672a24552ec64678eeb1d3a08d"
Expand Down Expand Up @@ -1080,6 +1085,11 @@ escape-string-regexp@^1.0.5:
resolved "https://registry.npmmirror.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==

escape-string-regexp@^2.0.0:
version "2.0.0"
resolved "https://registry.npmmirror.com/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz#a30304e99daa32e23b2fd20f51babd07cffca344"
integrity sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w==

eslint-import-resolver-node@^0.3.6:
version "0.3.6"
resolved "https://registry.npmmirror.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.6.tgz#4048b958395da89668252001dbd9eca6b83bacbd"
Expand Down Expand Up @@ -2820,6 +2830,13 @@ spdx-license-ids@^3.0.0:
resolved "https://registry.npmmirror.com/spdx-license-ids/-/spdx-license-ids-3.0.11.tgz#50c0d8c40a14ec1bf449bae69a0ea4685a9d9f95"
integrity sha512-Ctl2BrFiM0X3MANYgj3CkygxhRmr9mi6xhejbdO960nF6EDJApTYpn0BQnDKlnNBULKiCN1n3w9EBkHK8ZWg+g==

stack-utils@^2.0.5:
version "2.0.5"
resolved "https://registry.npmmirror.com/stack-utils/-/stack-utils-2.0.5.tgz#d25265fca995154659dbbfba3b49254778d2fdd5"
integrity sha512-xrQcmYhOsn/1kX+Vraq+7j4oE2j/6BFscZ0etmYg81xuM8Gq0022Pxb8+IqgOFUIaxHs0KaSb7T1+OegiNrNFA==
dependencies:
escape-string-regexp "^2.0.0"

string-width@^4.1.0, string-width@^4.2.0:
version "4.2.3"
resolved "https://registry.npmmirror.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
Expand Down

0 comments on commit 793e2fc

Please sign in to comment.