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

Function JestExtendPlugin

packages/expect/src/jest-extend.ts:85–216  ·  view source on GitHub ↗
(
  c: Chai.ChaiStatic,
  expect: ExpectStatic,
  matchers: MatchersObject,
)

Source from the content-addressed store, hash-verified

83}
84
85function JestExtendPlugin(
86 c: Chai.ChaiStatic,
87 expect: ExpectStatic,
88 matchers: MatchersObject,
89): ChaiPlugin {
90 return (_, utils) => {
91 Object.entries(matchers).forEach(
92 ([expectAssertionName, expectAssertion]) => {
93 function __VITEST_EXTEND_ASSERTION__(
94 this: Chai.AssertionStatic & Chai.Assertion,
95 ...args: any[]
96 ) {
97 const { state, isNot, obj, customMessage } = getMatcherState(this, expect)
98
99 const result = expectAssertion.call(state, obj, ...args)
100
101 if (
102 result
103 && typeof result === 'object'
104 && typeof (result as any).then === 'function'
105 ) {
106 const thenable = result as PromiseLike<SyncExpectationResult>
107 return thenable.then(({ pass, message, actual, expected, meta }) => {
108 if ((pass && isNot) || (!pass && !isNot)) {
109 const errorMessage = (customMessage ? `${customMessage}: ` : '') + message()
110 throw new JestExtendError(
111 errorMessage,
112 actual,
113 expected,
114 { assertionName: expectAssertionName, meta },
115 )
116 }
117 })
118 }
119
120 const { pass, message, actual, expected, meta } = result as SyncExpectationResult
121
122 if ((pass && isNot) || (!pass && !isNot)) {
123 const errorMessage = (customMessage ? `${customMessage}: ` : '') + message()
124 throw new JestExtendError(
125 errorMessage,
126 actual,
127 expected,
128 { assertionName: expectAssertionName, meta },
129 )
130 }
131 }
132
133 const softWrapper = wrapAssertion(utils, expectAssertionName, __VITEST_EXTEND_ASSERTION__)
134 utils.addMethod(
135 (globalThis as any)[JEST_MATCHERS_OBJECT].matchers,
136 expectAssertionName,
137 softWrapper,
138 )
139 utils.addMethod(
140 c.Assertion.prototype,
141 expectAssertionName,
142 softWrapper,

Callers 1

JestExtendFunction · 0.85

Calls 2

wrapAssertionFunction · 0.90
entriesMethod · 0.80

Tested by

no test coverage detected