MCPcopy
hub / github.com/vitest-dev/vitest / SnapshotPlugin

Function SnapshotPlugin

packages/vitest/src/integrations/snapshot/chai.ts:78–179  ·  view source on GitHub ↗
(chai, utils)

Source from the content-addressed store, hash-verified

76}
77
78export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
79 for (const key of ['matchSnapshot', 'toMatchSnapshot']) {
80 utils.addMethod(
81 chai.Assertion.prototype,
82 key,
83 wrapAssertion(utils, key, function (
84 this,
85 propertiesOrHint?: object | string,
86 hint?: string,
87 ) {
88 const result = toMatchSnapshotImpl({
89 assertion: this,
90 received: utils.flag(this, 'object'),
91 ...normalizeArguments(propertiesOrHint, hint),
92 })
93 return assertMatchResult(result, chai.util.flag(this, 'message'))
94 }),
95 )
96 }
97
98 utils.addMethod(
99 chai.Assertion.prototype,
100 'toMatchFileSnapshot',
101 function (this: Chai.Assertion, filepath: string, hint?: string) {
102 // set name manually since it's not wrapped by wrapAssertion
103 utils.flag(this, '_name', 'toMatchFileSnapshot')
104 // validate early synchronously just not to break some existing tests
105 validateAssertion(this)
106 const resultPromise = toMatchFileSnapshotImpl({
107 assertion: this,
108 received: utils.flag(this, 'object'),
109 filepath,
110 hint,
111 })
112 const assertPromise = resultPromise.then(result =>
113 assertMatchResult(result, chai.util.flag(this, 'message')),
114 )
115 return recordAsyncExpect(
116 getTest(this),
117 assertPromise,
118 createAssertionMessage(utils, this, true),
119 new Error('resolves'),
120 utils.flag(this, 'soft'),
121 )
122 },
123 )
124
125 utils.addMethod(
126 chai.Assertion.prototype,
127 'toMatchInlineSnapshot',
128 wrapAssertion(utils, 'toMatchInlineSnapshot', function __INLINE_SNAPSHOT_OFFSET_3__(
129 this,
130 propertiesOrInlineSnapshot?: object | string,
131 inlineSnapshotOrHint?: string,
132 hint?: string,
133 ) {
134 const result = toMatchSnapshotImpl({
135 assertion: this,

Callers

nothing calls this directly

Calls 11

wrapAssertionFunction · 0.90
recordAsyncExpectFunction · 0.90
createAssertionMessageFunction · 0.90
toMatchSnapshotImplFunction · 0.85
normalizeArgumentsFunction · 0.85
assertMatchResultFunction · 0.85
validateAssertionFunction · 0.85
toMatchFileSnapshotImplFunction · 0.85
getTestFunction · 0.85
normalizeInlineArgumentsFunction · 0.85
getErrorFunction · 0.70

Tested by

no test coverage detected