mirror of
https://github.com/vitest-dev/vitest.git
synced 2025-12-08 18:26:03 +00:00
refactor: helper for expect
This commit is contained in:
parent
e6f384f849
commit
1e89627246
@ -4,30 +4,30 @@ import { ChaiPlugin } from './types'
|
||||
// TODO: add more https://jestjs.io/docs/expect
|
||||
export function JestChaiExpect(): ChaiPlugin {
|
||||
return (chai, utils) => {
|
||||
const proto = chai.Assertion.prototype
|
||||
utils.addMethod(proto, 'toEqual', function(this: Chai.Assertion, expected: any) {
|
||||
function def(name: keyof Chai.Assertion, fn: ((this: Chai.AssertionStatic & Chai.Assertion, ...args: any[]) => any)) {
|
||||
utils.addMethod(chai.Assertion.prototype, name, fn)
|
||||
}
|
||||
|
||||
def('toEqual', function(expected) {
|
||||
return this.eql(expected)
|
||||
})
|
||||
utils.addMethod(proto, 'toStrictEqual', function(this: Chai.Assertion, expected: any) {
|
||||
def('toStrictEqual', function(expected) {
|
||||
return this.equal(expected)
|
||||
})
|
||||
utils.addMethod(proto, 'toBe', function(this: Chai.Assertion, expected: any) {
|
||||
def('toBe', function(expected) {
|
||||
return this.equal(expected)
|
||||
})
|
||||
utils.addMethod(proto, 'toMatchObject', function(this: Chai.Assertion, expected: any) {
|
||||
def('toMatchObject', function(expected) {
|
||||
return this.containSubset(expected)
|
||||
})
|
||||
utils.addMethod(proto, 'toMatch', function(this: Chai.Assertion, expected: string | RegExp) {
|
||||
def('toMatch', function(expected: string | RegExp) {
|
||||
if (typeof expected === 'string')
|
||||
return this.include(expected)
|
||||
else
|
||||
return this.match(expected)
|
||||
})
|
||||
utils.addMethod(proto, 'toContain', function(this: Chai.Assertion, item: any) {
|
||||
return this.contain(item)
|
||||
})
|
||||
|
||||
utils.addMethod(proto, 'toContainEqual', function(this: Chai.AssertionStatic, expected: any) {
|
||||
def('toContain', function(item) { return this.contain(item) })
|
||||
def('toContainEqual', function(expected) {
|
||||
const obj = utils.flag(this, 'object')
|
||||
const index = Array.from(obj).findIndex((item) => {
|
||||
try {
|
||||
@ -45,9 +45,8 @@ export function JestChaiExpect(): ChaiPlugin {
|
||||
'expected #{this} to not deep equally contain #{exp}',
|
||||
expected,
|
||||
)
|
||||
},
|
||||
)
|
||||
utils.addMethod(proto, 'toBeTruthy', function(this: Chai.AssertionStatic) {
|
||||
})
|
||||
def('toBeTruthy', function() {
|
||||
const obj = utils.flag(this, 'object')
|
||||
this.assert(
|
||||
Boolean(obj),
|
||||
@ -56,7 +55,7 @@ export function JestChaiExpect(): ChaiPlugin {
|
||||
obj,
|
||||
)
|
||||
})
|
||||
utils.addMethod(proto, 'toBeFalsy', function(this: Chai.AssertionStatic) {
|
||||
def('toBeFalsy', function() {
|
||||
const obj = utils.flag(this, 'object')
|
||||
this.assert(
|
||||
!obj,
|
||||
@ -65,33 +64,33 @@ export function JestChaiExpect(): ChaiPlugin {
|
||||
obj,
|
||||
)
|
||||
})
|
||||
utils.addMethod(proto, 'toBeNaN', function(this: Chai.Assertion) {
|
||||
def('toBeNaN', function() {
|
||||
return this.be.NaN
|
||||
})
|
||||
utils.addMethod(proto, 'toBeUndefined', function(this: Chai.Assertion) {
|
||||
def('toBeUndefined', function() {
|
||||
return this.be.undefined
|
||||
})
|
||||
utils.addMethod(proto, 'toBeNull', function(this: Chai.Assertion) {
|
||||
def('toBeNull', function() {
|
||||
return this.be.null
|
||||
})
|
||||
utils.addMethod(proto, 'toBeDefined', function(this: Chai.Assertion) {
|
||||
def('toBeDefined', function() {
|
||||
return this.not.be.undefined
|
||||
})
|
||||
utils.addMethod(proto, 'toBeInstanceOf', function(this: Chai.Assertion, obj: any) {
|
||||
def('toBeInstanceOf', function(obj: any) {
|
||||
return this.instanceOf(obj)
|
||||
})
|
||||
|
||||
// mock
|
||||
utils.addMethod(proto, 'toHaveBeenCalledTimes', function(this: Chai.Assertion, number: number) {
|
||||
def('toHaveBeenCalledTimes', function(number: number) {
|
||||
return this.callCount(number)
|
||||
})
|
||||
utils.addMethod(proto, 'toHaveBeenCalledOnce', function(this: Chai.Assertion) {
|
||||
def('toHaveBeenCalledOnce', function() {
|
||||
return this.callCount(1)
|
||||
})
|
||||
utils.addMethod(proto, 'toHaveBeenCalled', function(this: Chai.Assertion) {
|
||||
def('toHaveBeenCalled', function() {
|
||||
return this.called
|
||||
})
|
||||
utils.addMethod(proto, 'toHaveBeenCalledWith', function(this: Chai.Assertion, ...args: any[]) {
|
||||
def('toHaveBeenCalledWith', function(...args) {
|
||||
return this.calledWith(...args)
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user