Skip to content

Commit

Permalink
fix: correctly create snapshot for rejected errors (#1567)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 1, 2022
1 parent 6a5ce49 commit 7ad8e3f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
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

0 comments on commit 7ad8e3f

Please sign in to comment.