Skip to content

Async custom snapshot #10106

@ext

Description

@ext

Describe the bug

I was trying out the new experimental custom snapshots feature from v4.1.3 but it doesn't seem to work with async matchers. Now this could be a documentation issue but I think I've tried most variants without success.

As soon as a promise is returned this exception is thrown:

Error: @vitest/snapshot: Couldn't infer stack frame for inline snapshot.

Reproduction

npm init && npm i -D vitest

src/custom-snapshot.test.ts:

import { expect, test, Snapshots } from "vitest"; 

declare module 'vitest' {
		interface Assertion<T = any> {
				toMatchCustomSnapshot(snapshot?: string): T;
				toMatchAsyncCustomSnapshot(snapshot?: string): Promise<T>;
		}
}

function syncResult(value: string): string {
		return value;
}

function asyncResult(value: string): Promise<string> {
		return Promise.resolve(value); 
}

expect.extend({
		toMatchCustomSnapshot(received: string, inlineSnapshot?: string) {
				const snapshot = syncResult(received);
				return Snapshots.toMatchInlineSnapshot.call(this, snapshot, inlineSnapshot);
		},
		async toMatchAsyncCustomSnapshot(received: string, inlineSnapshot?: string) {
				const snapshot = await asyncResult(received);
				return Snapshots.toMatchInlineSnapshot.call(this, snapshot, inlineSnapshot);
		},
});

test('sync snapshot', () => {
		expect('foo').toMatchCustomSnapshot(`"foo"`);
});

test('async snapshot', async () => {
		await expect('foo').toMatchAsyncCustomSnapshot(`"foo"`);
});

Run with vitest run.

First test works as expected, the async one fails with the "Couldn't infer stack frame for inline snapshot".

System Info

System:
    OS: Linux 6.16 Gentoo Linux
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 32.56 GB / 62.70 GB
    Container: Yes
    Shell: 5.9.0.3 - /bin/zsh
  Binaries:
    Node: 24.11.1 - /usr/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 11.11.0 - /usr/bin/npm
    pnpm: 10.33.0 - /usr/bin/pnpm
    Deno: 2.7.4 - /usr/bin/deno
  Browsers:
    Chromium: 143.0.7499.109
    Firefox: 146.0
    Firefox Developer Edition: 146.0
  npmPackages:
    vitest: 4.1.3 => 4.1.3

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions