Skip to content

Commit

Permalink
fix: correctly generate inline snapshots inside js files (#1510)
Browse files Browse the repository at this point in the history
* fix: correctly generate inline snapshots inside js files

* chore: cleanup

* chore: add full link to comment

* test: imvrove snapshot generated test

* chore: remove snapshots :S

* chore: fix lockfile

* chore: generate inline snapshot test instead of using one
  • Loading branch information
sheremet-va committed Jun 21, 2022
1 parent 52e371a commit 9f9216f
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 210 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -17,4 +17,4 @@ cypress/videos
cypress/downloads
cypress/screenshots
docs/public/user-avatars
docs/public/sponsors
docs/public/sponsors
8 changes: 6 additions & 2 deletions packages/vitest/src/integrations/snapshot/port/state.ts
Expand Up @@ -87,7 +87,7 @@ export default class SnapshotState {
})
}

private _getInlineSnapshotStack(stacks: ParsedStack[]) {
private _inferInlineSnapshotStack(stacks: ParsedStack[]) {
// if called inside resolves/rejects, stacktrace is different
const promiseIndex = stacks.findIndex(i => i.method.match(/__VITEST_(RESOLVES|REJECTS)__/))
if (promiseIndex !== -1)
Expand All @@ -109,12 +109,16 @@ export default class SnapshotState {
const error = options.error || new Error('Unknown error')
const stacks = parseStacktrace(error, true)
stacks.forEach(i => i.file = slash(i.file))
const stack = this._getInlineSnapshotStack(stacks)
const stack = this._inferInlineSnapshotStack(stacks)
if (!stack) {
throw new Error(
`Vitest: Couldn't infer stack frame for inline snapshot.\n${JSON.stringify(stacks)}`,
)
}
// removing 1 column, because source map points to the wrong
// location for js files, but `column-1` points to the same in both js/ts
// https://github.com/vitejs/vite/issues/8657
stack.column--
this._inlineSnapshots.push({
snapshot: receivedSerialized,
...stack,
Expand Down

0 comments on commit 9f9216f

Please sign in to comment.