[vitest-plugin] `toMatchInlineSnapshot` updates are written into a later test's snapshot (source positions not mapped in workerd)

Open
#17,153 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
node.js, typescript, wasm

Research direction

Start from the vitest-plugin integration that services loopback snapshot requests and compare its positions with Vitest's saveInlineSnapshots/replaceInlineSnap path. Reproduce with the provided wrangler.jsonc, vitest.config.ts and test/repro.test.ts, then run npx vitest run -u and inspect the reported line/column against the source. Done means stack-trace positions inside workerd are mapped through source maps so the intended inline snapshot is rewritten and failure frames point at the source.

Written by the indexing model from the issue text.

Description

What versions & operating system are you using?
  • @cloudflare/vitest-plugin 1.9.2
  • vitest 5.1.1
  • wrangler 4.168.0
  • Node 22.23, Linux x64
Please provide a link to a minimal reproduction

See below

Describe the Bug
What happened?

Running vitest run -u on a test file in the Workers project, where one inline snapshot is out of date:

  • Vitest reports Snapshots 1 updated, and every test passes.
  • The new value is written into the toMatchInlineSnapshot(...) call of a later, unrelated test in the same file. The outdated snapshot is left as it was.
  • If no inline snapshot follows the outdated one, nothing is written at all, and the next run fails again.
  • A new, empty toMatchInlineSnapshot() is reported as written, but it is never written.

Failure frames are also misplaced: the line is wrong whenever the file has imports, and the column is always wrong.

The same test files run in a plain Node Vitest project behave correctly. File snapshots (toMatchSnapshot, toMatchFileSnapshot) work correctly in the Workers project.

Cause, as far as I can tell

I logged the loopback snapshot requests and Vitest's saveInlineSnapshots. The line and column Vitest receives for each inline snapshot are positions in the transformed module, not the source. For example, calls on lines 15 and 20 of the source were reported at lines 28 and 36. So source maps don't seem to be applied to stack traces inside workerd. replaceInlineSnap then searches forward from that offset and rewrites the first toMatchInlineSnapshot( it finds after it.

Reproduction

wrangler.jsonc:

{ "name": "repro", "main": "src/index.ts", "compatibility_date": "2026-11-01" }

src/index.ts:

export default {
  async fetch(): Promise<Response> {
    return new Response('ok', { headers: { 'cache-control': 'no-store' } });
  },
};

vitest.config.ts:

import { defineConfig } from 'vitest/config';
import { cloudflareTest } from '@cloudflare/vitest-plugin';
export default defineConfig({ plugins: [cloudflareTest({ wrangler: { configPath: './wrangler.jsonc' } })] });

test/repro.test.ts:

import { SELF } from 'cloudflare:test';
import { expect, it } from 'vitest';

it('the expectation that should change', async () => {
  const res = await SELF.fetch('https://example.com/');
  expect(res.headers.get('cache-control')).toMatchInlineSnapshot(`"stale value"`);
});

it('padding 1', () => {});
it('padding 2', () => {});
it('padding 3', () => {});
it('padding 4', () => {});
it('padding 5', () => {});

it('an unrelated expectation', () => {
  expect('untouched').toMatchInlineSnapshot(`"untouched"`);
});

Then (package.json has "type": "module"):

npx vitest run -u

Expected: the first test's snapshot becomes "no-store".

Actual: the output says Snapshots 1 updated, and all tests pass. The first test still says "stale value", and the last test now reads toMatchInlineSnapshot("no-store").

Related

#7288 reported wrong inline snapshot positions on Windows with vitest-pool-workers. It was closed by #11632 in March 2026. This report is on Linux with @cloudflare/vitest-plugin.

Please provide any relevant error logs

No response

Dominant language
TypeScript
Stars
4.7k
Forks
1.7k
Avg merge
1d 22h
Merged PRs (30d)
282

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workers-sdk

All issues in cloudflare/workers-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.