diff --git a/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts b/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts index 8f00e1eb855e..9b8bb9d2dd97 100644 --- a/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts +++ b/packages/vitest/src/integrations/snapshot/port/inlineSnapshot.ts @@ -26,7 +26,7 @@ export async function saveInlineSnapshots( const pos = await getOriginalPos(map, snap) const index = posToNumber(code, pos!) const { indent } = detectIndent(code.slice(index - pos!.column)) - replaceInlineSnap(code, s, index, snap.snapshot, indent + indent) + replaceInlineSnap(code, s, index, snap.snapshot, indent) } const transformed = s.toString() diff --git a/packages/vitest/src/integrations/snapshot/port/state.ts b/packages/vitest/src/integrations/snapshot/port/state.ts index 76a6d14c262c..f8a5ffde3d2a 100644 --- a/packages/vitest/src/integrations/snapshot/port/state.ts +++ b/packages/vitest/src/integrations/snapshot/port/state.ts @@ -97,7 +97,8 @@ export default class SnapshotState { const error = options.error || new Error('Unknown error') const stacks = parseStacktrace(error) stacks.forEach(i => i.file = slash(i.file)) - const stack = stacks.find(i => process.__vitest_worker__.ctx.files.includes(i.file)) + const stackIndex = stacks.findIndex(i => i.method === 'Proxy.methodWrapper') + const stack = stackIndex !== -1 ? stacks[stackIndex + 1] : null if (!stack) { throw new Error( 'Vitest: Couldn\'t infer stack frame for inline snapshot.', diff --git a/test/core/test/snapshot.test.ts b/test/core/test/snapshot.test.ts index 6630a5e46cc9..7ba7c2071e80 100644 --- a/test/core/test/snapshot.test.ts +++ b/test/core/test/snapshot.test.ts @@ -1,4 +1,5 @@ import { expect, test } from 'vitest' +import { testOutsideInlineSnapshot } from './snapshots-outside' test('snapshot', () => { expect({ @@ -6,6 +7,10 @@ test('snapshot', () => { }).toMatchSnapshot() }) +test('outside snapshot', () => { + testOutsideInlineSnapshot() +}) + test('inline snapshot', () => { expect('inline string').toMatchInlineSnapshot('"inline string"') expect({ foo: { type: 'object', map: new Map() } }).toMatchInlineSnapshot(` diff --git a/test/core/test/snapshots-outside.ts b/test/core/test/snapshots-outside.ts new file mode 100644 index 000000000000..b6b01991a709 --- /dev/null +++ b/test/core/test/snapshots-outside.ts @@ -0,0 +1,11 @@ +import { expect } from 'vitest' + +export function testOutsideInlineSnapshot() { + return (() => { + expect({ foo: 'bar' }).toMatchInlineSnapshot(` + { + "foo": "bar", + } + `) + })() +}