Skip to content

Commit

Permalink
fix: add stack traces to test message for test error. (#545)
Browse files Browse the repository at this point in the history
- For thrown error scenarios provides the location of the error thrown inside the tested code with fully navigable trace.
- For non throw error scenarios provides additional context on the failed assertion.

Co-authored-by: ArthurHub <[email protected]>
  • Loading branch information
ArthurHub and ArthurHub authored Nov 28, 2024
1 parent d605dc2 commit cdca42f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions samples/basic/src/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@

export function add(a: number, b: number) {
return a + b
}

export function sum(from: number, to: number) {
return (from + to) * (to - from + 1) / 2
}

export function addError(from: number, to: number) {
doSomething()
return add(from, to)
}

function doSomething() {
throw new Error('Something went wrong');
}
12 changes: 12 additions & 0 deletions samples/basic/test/throw.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, it } from 'vitest';
import { addError } from '../src/add';

describe('throw error', () => {
it('passes expecting an error to be thrown', () => {
expect(()=>addError(1, 1)).toThrow()
})

it('fails with error thrown', () => {
expect(addError(1, 1)).toBe(2)
})
})
13 changes: 13 additions & 0 deletions src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ function testMessageForTestError(testItem: vscode.TestItem, error: TestError | u
else
testMessage = new vscode.TestMessage(stripVTControlCharacters(error.message) ?? '')

testMessage.stackTrace = parseMessageStackFramesFromErrorStacks(error.stacks)

Check failure on line 510 in src/runner/runner.ts

View workflow job for this annotation

GitHub Actions / ci (macos-14, 20.x)

Property 'stackTrace' does not exist on type 'TestMessage'.

const location = parseLocationFromStacks(testItem, error.stacks ?? [])
if (location) {
const position = new vscode.Position(location.line - 1, location.column - 1)
Expand Down Expand Up @@ -550,6 +552,17 @@ function parseLocationFromStacks(testItem: vscode.TestItem, stacks: ParsedStack[
log.verbose?.('Could not find a valid stack for', testItem.label, JSON.stringify(stacks, null, 2))
}

function parseMessageStackFramesFromErrorStacks(stacks: ParsedStack[] | undefined): vscode.TestMessageStackFrame[] | undefined {

Check failure on line 555 in src/runner/runner.ts

View workflow job for this annotation

GitHub Actions / ci (macos-14, 20.x)

Namespace '"vscode"' has no exported member 'TestMessageStackFrame'.
if (!stacks || stacks.length === 0)
return undefined

return stacks.map((stack) => {
const { sourceFilepath, line, column } = getSourceFilepathAndLocationFromStack(stack)
const sourceUri = sourceFilepath ? vscode.Uri.file(sourceFilepath) : undefined
return new vscode.TestMessageStackFrame(stack.method, sourceUri, new vscode.Position(line - 1, column - 1))

Check failure on line 562 in src/runner/runner.ts

View workflow job for this annotation

GitHub Actions / ci (macos-14, 20.x)

Property 'TestMessageStackFrame' does not exist on type 'typeof import("vscode")'.
})
}

function getTestFiles(tests: readonly vscode.TestItem[]): string[] | SerializedTestSpecification[] {
// if there is a folder, we can't limit the tests to a specific project
const hasFolder = tests.some(test => getTestData(test) instanceof TestFolder)
Expand Down

0 comments on commit cdca42f

Please sign in to comment.