Skip to content

vi.defineHelper's error stack points inside helper when called as an method #10135

@hi-ogawa

Description

@hi-ogawa

Describe the bug

Example:

import { test, vi } from "vitest"

const cli = {
  waitForOutput: vi.defineHelper(async () => {
    throw new Error('foo')
  })
}

test('bad', async () => {
  await cli.waitForOutput()
});

test('good', async () => {
  const { waitForOutput } = cli
  await waitForOutput()
});
❯ test/repro.test.ts (2 tests | 2 failed) 3ms
   × bad 1ms
   × good 1ms

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/repro.test.ts:9:1 > bad
Error: foo
 ❯ test/repro.test.ts:5:11
      3| const cli = {
      4|   waitForOutput: vi.defineHelper(async () => {
      5|     throw new Error('foo')
       |           ^
      6|   })
      7| }
 ❯ test/repro.test.ts:10:13
 ❯ new Promise ../../../blitz.4c73681d.js:31:30606
 ❯ new Promise ../../../blitz.4c73681d.js:31:30606

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/2]⎯

 FAIL  test/repro.test.ts:13:1 > good
Error: foo
 ❯ test/repro.test.ts:15:9
     13| test('good', async () => {
     14|   const { waitForOutput } = cli
     15|   await waitForOutput()
       |         ^
     16| });
     17|
 ❯ new Promise ../../../blitz.4c73681d.js:31:30606
 ❯ new Promise ../../../blitz.4c73681d.js:31:30606

I've tested with the PR #10127 and it doesn't seem to fix, so likely this is a different issue about stack method naming.

Side note: I was tinkering to dog-food vi.defineHelper in our test utils, but apparently this doesn't yet work as is.

private waitForOutput(expected: string, source: Source, caller: Parameters<typeof Error.captureStackTrace>[1]) {
const error = new Error('Timeout')
Error.captureStackTrace(error, caller)

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-ew6hi9an?file=test%2Frepro.test.ts

System Info

Stackblitz
Vitest v4.1.4

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions