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

Function JestChaiExpect

packages/expect/src/jest-expect.ts:38–1216  ·  view source on GitHub ↗
(chai, utils)

Source from the content-addressed store, hash-verified

36
37// Jest Expect Compact
38export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
39 const { AssertionError } = chai
40 const customTesters = getCustomEqualityTesters()
41
42 function def(
43 name: keyof Assertion | (keyof Assertion)[],
44 fn: (this: Chai.AssertionStatic & Assertion, ...args: any[]) => any,
45 ) {
46 const addMethod = (n: keyof Assertion) => {
47 const softWrapper = wrapAssertion(utils, n, fn)
48 utils.addMethod(chai.Assertion.prototype, n, softWrapper)
49 utils.addMethod(
50 (globalThis as any)[JEST_MATCHERS_OBJECT].matchers,
51 n,
52 softWrapper,
53 )
54 }
55
56 if (Array.isArray(name)) {
57 name.forEach(n => addMethod(n))
58 }
59 else {
60 addMethod(name)
61 }
62 }
63
64 (['throw', 'throws', 'Throw'] as const).forEach((m) => {
65 utils.overwriteMethod(chai.Assertion.prototype, m, (_super: any) => {
66 return function (
67 this: Chai.Assertion & Chai.AssertionStatic,
68 ...args: any[]
69 ) {
70 const promise = utils.flag(this, 'promise')
71 const object = utils.flag(this, 'object')
72 const isNot = utils.flag(this, 'negate') as boolean
73 if (promise === 'rejects') {
74 utils.flag(this, 'object', () => {
75 throw object
76 })
77 }
78 // if it got here, it's already resolved
79 // unless it tries to resolve to a function that should throw
80 // called as '.resolves[.not].toThrow()`
81 else if (promise === 'resolves' && typeof object !== 'function') {
82 if (!isNot) {
83 const message
84 = utils.flag(this, 'message')
85 || 'expected promise to throw an error, but it didn\'t'
86 const error = {
87 showDiff: false,
88 }
89 throw new AssertionError(message, error, utils.flag(this, 'ssfi'))
90 }
91 else {
92 return
93 }
94 }
95 _super.apply(this, args)

Callers

nothing calls this directly

Calls 15

getCustomEqualityTestersFunction · 0.90
generateToBeMessageFunction · 0.90
getObjectSubsetFunction · 0.90
assertTypesFunction · 0.90
ordinalFunction · 0.90
isMockFunctionFunction · 0.90
isErrorFunction · 0.90
recordAsyncExpectFunction · 0.90
createAssertionMessageFunction · 0.90
defFunction · 0.85
getValueFunction · 0.85
getSpyFunction · 0.85

Tested by

no test coverage detected