From 4f6e39c1af1953b2cd6c45b49383af955c5b08e0 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Fri, 15 Mar 2024 10:35:21 +0100 Subject: [PATCH] fix: correct locations in test.each tasks --- packages/browser/src/client/runner.ts | 2 +- packages/runner/src/suite.ts | 16 ++++++++++++---- test/public-api/fixtures/custom.spec.ts | 4 ++++ test/public-api/tests/runner.spec.ts | 11 ++++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/browser/src/client/runner.ts b/packages/browser/src/client/runner.ts index 1725b93883d2..f316715a541d 100644 --- a/packages/browser/src/client/runner.ts +++ b/packages/browser/src/client/runner.ts @@ -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) diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index 0f5feb6b787d..b1761b5c20fc 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -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 } @@ -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, } } } @@ -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) @@ -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, } } } diff --git a/test/public-api/fixtures/custom.spec.ts b/test/public-api/fixtures/custom.spec.ts index 6d604c5b15a4..866ee53f9a0e 100644 --- a/test/public-api/fixtures/custom.spec.ts +++ b/test/public-api/fixtures/custom.spec.ts @@ -14,3 +14,7 @@ afterAll((suite) => { test('custom', ({ task }) => { task.meta.custom = 'some-custom-hanlder' }) + +test.each([1, 2])('custom %s', () => { + // support locations +}) diff --git a/test/public-api/tests/runner.spec.ts b/test/public-api/tests/runner.spec.ts index e8160d4aa7f9..963c9d06c3fe 100644 --- a/test/public-api/tests/runner.spec.ts +++ b/test/public-api/tests/runner.spec.ts @@ -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() || [] @@ -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, + }) + }) })