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: correctly create snapshot for rejected errors #1567

Merged
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
28 changes: 21 additions & 7 deletions packages/vitest/src/integrations/snapshot/chai.ts
Expand Up @@ -10,15 +10,27 @@ export function getSnapshotClient(): SnapshotClient {
return _client
}

const getErrorString = (expected: () => void) => {
const getErrorMessage = (err: unknown) => {
if (err instanceof Error)
return err.message

return err
}

const getErrorString = (expected: () => void | Error, promise: string | undefined) => {
if (typeof expected !== 'function') {
if (!promise)
throw new Error(`expected must be a function, received ${typeof expected}`)

// when "promised", it receives thrown error
return getErrorMessage(expected)
}

try {
expected()
}
catch (e) {
if (e instanceof Error)
return e.message

return e
return getErrorMessage(e)
}

throw new Error('snapshot function didn\'t threw')
Expand Down Expand Up @@ -81,9 +93,10 @@ 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')
const promise = utils.flag(this, 'promise') as string | undefined
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: getErrorString(expected),
received: getErrorString(expected, promise),
test,
message,
errorMessage,
Expand All @@ -97,9 +110,10 @@ 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')
const promise = utils.flag(this, 'promise') as string | undefined
const errorMessage = utils.flag(this, 'message')
getSnapshotClient().assert({
received: getErrorString(expected),
received: getErrorString(expected, promise),
test,
message,
inlineSnapshot,
Expand Down
2 changes: 2 additions & 0 deletions test/core/test/__snapshots__/snapshot.test.ts.snap
Expand Up @@ -36,6 +36,8 @@ exports[`throwing 3`] = `
}
`;

exports[`throwing 4`] = `"omega"`;

exports[`with big array 1`] = `
{
"this": {
Expand Down
12 changes: 11 additions & 1 deletion test/core/test/snapshot-inline.test.ts
Expand Up @@ -59,7 +59,7 @@ test('template literal', () => {
`)
})

test('throwing inline snapshots', () => {
test('throwing inline snapshots', async () => {
expect(() => {
throw new Error('omega')
}).toThrowErrorMatchingInlineSnapshot('"omega"')
Expand Down Expand Up @@ -99,6 +99,16 @@ test('throwing inline snapshots', () => {
with
newlines"
`)

await expect(async () => {
throw new Error('omega')
}).rejects.toThrowErrorMatchingInlineSnapshot('"omega"')
})

test('throwing expect should be a function', async () => {
expect(() => {
expect(new Error('omega')).toThrowErrorMatchingInlineSnapshot()
}).toThrow(/expected must be a function/)
})

test('properties inline snapshot', () => {
Expand Down
12 changes: 11 additions & 1 deletion test/core/test/snapshot.test.ts
Expand Up @@ -30,7 +30,7 @@ test('with big string', () => {
}).toMatchSnapshot()
})

test('throwing', () => {
test('throwing', async () => {
expect(() => {
throw new Error('omega')
}).toThrowErrorMatchingSnapshot()
Expand All @@ -44,6 +44,16 @@ test('throwing', () => {
// eslint-disable-next-line no-throw-literal
throw { error: 'omega' }
}).toThrowErrorMatchingSnapshot()

await expect(async () => {
throw new Error('omega')
}).rejects.toThrowErrorMatchingSnapshot()
})

test('throwing expect should be a function', async () => {
expect(() => {
expect(new Error('omega')).toThrowErrorMatchingSnapshot()
}).toThrow(/expected must be a function/)
})

test('properties snapshot', () => {
Expand Down