Skip to content

Commit

Permalink
fix: inline snapshot if not called inside suite
Browse files Browse the repository at this point in the history
closes #484
  • Loading branch information
sheremet-va committed Jan 9, 2022
1 parent 7782e7d commit 6d743c5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/integrations/snapshot/port/state.ts
Expand Up @@ -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.',
Expand Down
5 changes: 5 additions & 0 deletions test/core/test/snapshot.test.ts
@@ -1,11 +1,16 @@
import { expect, test } from 'vitest'
import { testOutsideInlineSnapshot } from './snapshots-outside'

test('snapshot', () => {
expect({
this: { is: new Set(['of', 'snapshot']) },
}).toMatchSnapshot()
})

test('outside snapshot', () => {
testOutsideInlineSnapshot()
})

test('inline snapshot', () => {
expect('inline string').toMatchInlineSnapshot('"inline string"')
expect({ foo: { type: 'object', map: new Map() } }).toMatchInlineSnapshot(`
Expand Down
11 changes: 11 additions & 0 deletions test/core/test/snapshots-outside.ts
@@ -0,0 +1,11 @@
import { expect } from 'vitest'

export function testOutsideInlineSnapshot() {
return (() => {
expect({ foo: 'bar' }).toMatchInlineSnapshot(`
{
"foo": "bar",
}
`)
})()
}

0 comments on commit 6d743c5

Please sign in to comment.