Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show custom error message if snapshot failed #1237

Merged
merged 1 commit into from May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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