Skip to content

Commit

Permalink
fix: show custom error message if snapshot failed (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 5, 2022
1 parent d15d47f commit a955655
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
41 changes: 37 additions & 4 deletions packages/vitest/src/integrations/snapshot/chai.ts
Expand Up @@ -36,7 +36,15 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
message = properties
properties = undefined
}
getSnapshotClient().assert(expected, test, message, false, properties)
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: expected,
test,
message,
isInline: false,
properties,
errorMessage,
})
},
)
}
Expand All @@ -54,7 +62,17 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
}
if (inlineSnapshot)
inlineSnapshot = stripSnapshotIndentation(inlineSnapshot)
getSnapshotClient().assert(expected, test, message, true, properties, inlineSnapshot, error)
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: expected,
test,
message,
isInline: true,
properties,
inlineSnapshot,
error,
errorMessage,
})
},
)
utils.addMethod(
Expand All @@ -63,7 +81,13 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
function (this: Record<string, unknown>, message?: string) {
const expected = utils.flag(this, 'object')
const test = utils.flag(this, 'vitest-test')
getSnapshotClient().assert(getErrorString(expected), test, message)
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: getErrorString(expected),
test,
message,
errorMessage,
})
},
)
utils.addMethod(
Expand All @@ -73,7 +97,16 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
const expected = utils.flag(this, 'object')
const error = utils.flag(this, 'error')
const test = utils.flag(this, 'vitest-test')
getSnapshotClient().assert(getErrorString(expected), test, message, true, undefined, inlineSnapshot, error)
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: getErrorString(expected),
test,
message,
inlineSnapshot,
isInline: true,
error,
errorMessage,
})
},
)
}
28 changes: 25 additions & 3 deletions packages/vitest/src/integrations/snapshot/client.ts
Expand Up @@ -12,6 +12,17 @@ export interface Context {
fullTitle?: string
}

interface AssertOptions {
received: unknown
test?: Test
message?: string
isInline?: boolean
properties?: object
inlineSnapshot?: string
error?: Error
errorMessage?: string
}

export class SnapshotClient {
test: Test | undefined
snapshotState: SnapshotState | undefined
Expand Down Expand Up @@ -46,7 +57,18 @@ export class SnapshotClient {
this.test = undefined
}

assert(received: unknown, test = this.test, message?: string, isInline = false, properties?: object, inlineSnapshot?: string, error?: Error): void {
assert(options: AssertOptions): void {
const {
test = this.test,
message,
isInline = false,
properties,
inlineSnapshot,
error,
errorMessage,
} = options
let { received } = options

if (!test)
throw new Error('Snapshot cannot be used outside of test')

Expand All @@ -62,7 +84,7 @@ export class SnapshotClient {
received = deepMergeSnapshot(received, properties)
}
catch (err: any) {
err.message = 'Snapshot mismatched'
err.message = errorMessage || 'Snapshot mismatched'
throw err
}
}
Expand All @@ -87,7 +109,7 @@ export class SnapshotClient {
expect(actual.trim()).equals(expected ? expected.trim() : '')
}
catch (error: any) {
error.message = `Snapshot \`${key || 'unknown'}\` mismatched`
error.message = errorMessage || `Snapshot \`${key || 'unknown'}\` mismatched`
throw error
}
}
Expand Down

0 comments on commit a955655

Please sign in to comment.