Skip to content

Conversation

@hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Feb 5, 2026

Description

Resolves #4494

This PR implements vi.defineHelper, which works like this:

import { expect, vi } from 'vitest'

const assertPair = vi.defineHelper((a, b) => {
  expect(a).toEqual(b)  // 🙅‍♂️ error code block will NOT point to here
})

test('example', () => {
  assertPair('left', 'right')  // 🙆 but point to here
})

Example output:

FAIL  example.test.ts > example
AssertionError: expected 'left' to deeply equal 'right'

Expected: "right"
Received: "left"

  example.test.ts:8:3
      7| test('example', () => {
      8|   assertPair('left', 'right')
       |   ^
      9| })

The implementation relies on error stack error filtering based on special marker function __VITEST_HELPER__. This processing happens during parseStacktrace, so the filtering also applies to --printConsoleTrace (which I just realized and maybe we can tweak the behavior.)

The error stack and __VITEST_HELPER__ filtering requires the original error to have proper trace. It turns out Webkit is a bit behind other runtimes, so currently "async" helper doesn't work. But, it looks like it's coming soon as Bun has it already WebKit/WebKit#57832 and this is also reached main line webkit oven-sh/bun#22517. We'll likely just need to wait for a few weeks until Playwright webkit update and see what happens.

TODO

  • test
    • console
  • doc

TBD

  • should printConsoleTrace also hide helper internal?
  • rename to vi.defineHelper

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify
Copy link

netlify bot commented Feb 5, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit d2a7d36
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6986a3e9bd81bb00082705c6
😎 Deploy Preview https://deploy-preview-9594--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@hi-ogawa hi-ogawa added this to the 4.1.0 milestone Feb 6, 2026
@hi-ogawa hi-ogawa moved this to P2 - 3 in Team Board Feb 6, 2026
@hi-ogawa hi-ogawa added the p2-to-be-discussed Enhancement under consideration (priority) label Feb 6, 2026
@hi-ogawa hi-ogawa marked this pull request as ready for review February 6, 2026 03:23
@hi-ogawa hi-ogawa moved this from P2 - 3 to Approved in Team Board Feb 6, 2026
@hi-ogawa hi-ogawa self-assigned this Feb 6, 2026
hi-ogawa and others added 3 commits February 7, 2026 11:28
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Feb 7, 2026

The API is renamed to vi.defineHelper.

@sheremet-va
Copy link
Member

LGMT!

@sheremet-va sheremet-va merged commit eeb0ae2 into vitest-dev:main Feb 7, 2026
25 of 28 checks passed
@hi-ogawa hi-ogawa deleted the 02-05-feat_assertion_helper_to_filter_out_internal_stack_traces branch February 8, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2-to-be-discussed Enhancement under consideration (priority)

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

Test line that fails is well hidden if a helper method fails

2 participants