forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: apply serializer to
Error
instance for thrown snapshot (vitest…
…-dev#4396) Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
- Loading branch information
1 parent
d09b606
commit 77510f3
Showing
13 changed files
with
148 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
test('basic', () => { | ||
// example from docs/guide/snapshot.md | ||
|
||
const bar = { | ||
foo: { | ||
x: 1, | ||
y: 2, | ||
}, | ||
} | ||
|
||
// without custom serializer | ||
expect(bar).toMatchInlineSnapshot(` | ||
{ | ||
"foo": { | ||
"x": 1, | ||
"y": 2, | ||
}, | ||
} | ||
`) | ||
|
||
// with custom serializer | ||
expect.addSnapshotSerializer({ | ||
serialize(val, config, indentation, depth, refs, printer) { | ||
return `Pretty foo: ${printer( | ||
val.foo, | ||
config, | ||
indentation, | ||
depth, | ||
refs, | ||
)}` | ||
}, | ||
test(val) { | ||
return val && Object.prototype.hasOwnProperty.call(val, 'foo') | ||
}, | ||
}) | ||
|
||
expect(bar).toMatchInlineSnapshot(` | ||
Pretty foo: { | ||
"x": 1, | ||
"y": 2, | ||
} | ||
`) | ||
}) | ||
|
||
test('throwning snapshot', () => { | ||
// example from https://github.com/vitest-dev/vitest/issues/3655 | ||
|
||
class ErrorWithDetails extends Error { | ||
readonly details: unknown | ||
|
||
constructor(message: string, options: ErrorOptions & { details: unknown }) { | ||
super(message, options) | ||
this.details = options.details | ||
} | ||
} | ||
|
||
// without custom serializer | ||
const error = new ErrorWithDetails('some-error', { | ||
details: 'some-detail', | ||
}) | ||
expect(error).toMatchInlineSnapshot(`[Error: some-error]`) | ||
expect(() => { | ||
throw error | ||
}).toThrowErrorMatchingInlineSnapshot(`[Error: some-error]`) | ||
|
||
// with custom serializer | ||
expect.addSnapshotSerializer({ | ||
serialize(val, config, indentation, depth, refs, printer) { | ||
const error = val as ErrorWithDetails | ||
return `Pretty ${error.message}: ${printer( | ||
error.details, | ||
config, | ||
indentation, | ||
depth, | ||
refs, | ||
)}` | ||
}, | ||
test(val) { | ||
return val && val instanceof ErrorWithDetails | ||
}, | ||
}) | ||
expect(error).toMatchInlineSnapshot(`Pretty some-error: "some-detail"`) | ||
expect(() => { | ||
throw error | ||
}).toThrowErrorMatchingInlineSnapshot(`Pretty some-error: "some-detail"`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters