Skip to content

Commit

Permalink
(feat): make it work for browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Jan 22, 2024
1 parent e7a38e0 commit 79056fc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/matchers/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,41 @@ export function toMatchSnapshot(received: unknown, message: string) {
}

export function toMatchInlineSnapshot(received: unknown, inlineSnapshot: string, message: string) {
/**
* When running component/unit tests in the browser we receive a stack trace
* through the `this` scope.
*/
const browserErrorStack: string = this.errorStack

function __INLINE_SNAPSHOT__(inlineSnapshot: string, message: string) {
/**
* create a error object to pass along that helps Vitest's snapshot manager
* to infer the stack trace and locate the inline snapshot
*/
const error = new Error('inline snapshot')

/**
* merge stack traces from browser and node
*/
if (browserErrorStack && error.stack) {
error.stack = [
...error.stack.split('\n').slice(0, 3),
...browserErrorStack
.split('\n')
.slice(2)
.map((line) => line
/**
* stack traces within the browser have an url path, e.g.
* `http://localhost:8080/@fs/path/to/__tests__/unit/snapshot.test.js:123:45`
* that we want to remove so that the stack trace is properly
* parsed by Vitest, e.g. make it to:
* `/__tests__/unit/snapshot.test.js:123:45`
*/
.replace(/http:\/\/localhost:\d+/g, '')
.replace('/@fs/', '/')
)
].join('\n');
}
error.stack = error.stack?.split('\n').filter((line) => (
line.includes('__INLINE_SNAPSHOT__') ||
!(
Expand Down

0 comments on commit 79056fc

Please sign in to comment.