Skip to content

Commit 4f6e39c

Browse files
committedMar 15, 2024··
fix: correct locations in test.each tasks
1 parent c652148 commit 4f6e39c

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed
 

‎packages/browser/src/client/runner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ async function updateFilesLocations(files: File[]) {
127127
if (task.location) {
128128
const { line, column } = originalPositionFor(traceMap, task.location)
129129
if (line != null && column != null)
130-
task.location = { line, column: column + 1 }
130+
task.location = { line, column: task.each ? column : column + 1 }
131131
}
132132
if ('tasks' in task)
133133
task.tasks.forEach(updateLocation)

‎packages/runner/src/suite.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
153153
Error.stackTraceLimit = 10
154154
const error = new Error('stacktrace').stack!
155155
Error.stackTraceLimit = limit
156-
const stack = findStackTrace(error)
156+
const stack = findStackTrace(error, task.each ?? false)
157157
if (stack)
158158
task.location = stack
159159
}
@@ -226,7 +226,9 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
226226
if (stack) {
227227
suite.location = {
228228
line: stack.line,
229-
column: stack.column,
229+
// because source map is boundary based, this line leads to ")" in test.each()[(]),
230+
// but it should be the next opening bracket - here we assume it's on the same line
231+
column: each ? stack.column + 1 : stack.column,
230232
}
231233
}
232234
}
@@ -430,7 +432,7 @@ function formatTemplateString(cases: any[], args: any[]): any[] {
430432
return res
431433
}
432434

433-
function findStackTrace(error: string) {
435+
function findStackTrace(error: string, each: boolean) {
434436
// first line is the error message
435437
// and the first 3 stacks are always from the collector
436438
const lines = error.split('\n').slice(4)
@@ -439,7 +441,13 @@ function findStackTrace(error: string) {
439441
if (stack && stack.file === getTestFilepath()) {
440442
return {
441443
line: stack.line,
442-
column: stack.column,
444+
/**
445+
* test.each([1, 2])('name')
446+
* ^ leads here, but should
447+
* ^ lead here
448+
* in source maps it's the same boundary, so it just points to the start of it
449+
*/
450+
column: each ? stack.column + 1 : stack.column,
443451
}
444452
}
445453
}

‎test/public-api/fixtures/custom.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ afterAll((suite) => {
1414
test('custom', ({ task }) => {
1515
task.meta.custom = 'some-custom-hanlder'
1616
})
17+
18+
test.each([1, 2])('custom %s', () => {
19+
// support locations
20+
})

‎test/public-api/tests/runner.spec.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ it.each([
4747
const suiteMeta = { done: true }
4848
const testMeta = { custom: 'some-custom-hanlder' }
4949

50-
expect(taskUpdate).toHaveLength(2)
50+
expect(taskUpdate).toHaveLength(4)
5151
expect(finishedFiles).toHaveLength(1)
5252

5353
const files = vitest?.state.getFiles() || []
@@ -87,4 +87,13 @@ it.each([
8787
line: 14,
8888
column: 1,
8989
})
90+
91+
const eachTests = [1, 2]
92+
eachTests.forEach((name, index) => {
93+
expect(files[0].tasks[index + 1].name).toBe(`custom ${name}`)
94+
expect(files[0].tasks[index + 1].location).toEqual({
95+
line: 18,
96+
column: 18,
97+
})
98+
})
9099
})

0 commit comments

Comments
 (0)
Please sign in to comment.