Skip to content

Commit

Permalink
fix: correct locations in test.each tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 15, 2024
1 parent c652148 commit 4f6e39c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/browser/src/client/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async function updateFilesLocations(files: File[]) {
if (task.location) {
const { line, column } = originalPositionFor(traceMap, task.location)
if (line != null && column != null)
task.location = { line, column: column + 1 }
task.location = { line, column: task.each ? column : column + 1 }
}
if ('tasks' in task)
task.tasks.forEach(updateLocation)
Expand Down
16 changes: 12 additions & 4 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
Error.stackTraceLimit = 10
const error = new Error('stacktrace').stack!
Error.stackTraceLimit = limit
const stack = findStackTrace(error)
const stack = findStackTrace(error, task.each ?? false)
if (stack)
task.location = stack
}
Expand Down Expand Up @@ -226,7 +226,9 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
if (stack) {
suite.location = {
line: stack.line,
column: stack.column,
// because source map is boundary based, this line leads to ")" in test.each()[(]),
// but it should be the next opening bracket - here we assume it's on the same line
column: each ? stack.column + 1 : stack.column,
}
}
}
Expand Down Expand Up @@ -430,7 +432,7 @@ function formatTemplateString(cases: any[], args: any[]): any[] {
return res
}

function findStackTrace(error: string) {
function findStackTrace(error: string, each: boolean) {
// first line is the error message
// and the first 3 stacks are always from the collector
const lines = error.split('\n').slice(4)
Expand All @@ -439,7 +441,13 @@ function findStackTrace(error: string) {
if (stack && stack.file === getTestFilepath()) {
return {
line: stack.line,
column: stack.column,
/**
* test.each([1, 2])('name')
* ^ leads here, but should
* ^ lead here
* in source maps it's the same boundary, so it just points to the start of it
*/
column: each ? stack.column + 1 : stack.column,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/public-api/fixtures/custom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ afterAll((suite) => {
test('custom', ({ task }) => {
task.meta.custom = 'some-custom-hanlder'
})

test.each([1, 2])('custom %s', () => {
// support locations
})
11 changes: 10 additions & 1 deletion test/public-api/tests/runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ it.each([
const suiteMeta = { done: true }
const testMeta = { custom: 'some-custom-hanlder' }

expect(taskUpdate).toHaveLength(2)
expect(taskUpdate).toHaveLength(4)
expect(finishedFiles).toHaveLength(1)

const files = vitest?.state.getFiles() || []
Expand Down Expand Up @@ -87,4 +87,13 @@ it.each([
line: 14,
column: 1,
})

const eachTests = [1, 2]
eachTests.forEach((name, index) => {
expect(files[0].tasks[index + 1].name).toBe(`custom ${name}`)
expect(files[0].tasks[index + 1].location).toEqual({
line: 18,
column: 18,
})
})
})

0 comments on commit 4f6e39c

Please sign in to comment.