not: 類似于 ! 取反
expect(2).not.toBe(1)
toBe:類似 ===
expect(1).toBe(1)
toEqual:基本用來笔喉,匹配對象是否相等
expect({ one: 1 }).toEqual({ one: 1 })
toBeNull:用來匹配某個值 等于 null
expect(null).toBeNull(null)
toBeUndefined:用來匹配某個值 等于 undefined
expect(undefined).toBeUndefined(undefined)
toBeDeUndefined: 用來匹配某個值 不等于 undefined
expect('').toBeDeUndefined()
toBeTruthy: 用來匹配某個值 等于 真
expect(true).toBeTruthy()
toBeFalsy: 用來匹配某個值 等于 假
expect(false).toBeFalsy()
跟數(shù)字有關(guān)的匹配器
toBeGreaterThan 2>1 比某個數(shù)字大
expect(2).toBeGreaterThan(1)
toBeLessThan 1<2 比某個數(shù)字小
expect(1).toBeLessThan(2)
toBeGreaterThanOrEqual 2>=2 大于等于某個數(shù)字
expect(2).toBeGreaterThanOrEqual(2)
toBeLessThanOrEqual 2<=2 小于等于某個數(shù)字
expect(2).toBeLessThanOrEqual(2)
toBeCloseTo 用來計算浮點數(shù)相加是否相等
注:在js中小數(shù)相加 0.1 + 0.2 = 0.300000004 不知道會出現(xiàn)多少個0
expect(0.1 + 0.2).toBe(0.3) // 這個通不過測試
expect(0.1 + 0.2).toBeCloseTo(0.3) // 這樣就可以了
字符串相關(guān)的
toMatch 匹配 字符串內(nèi)是否包含(可以寫正則表達(dá)式) 某個字符串
expect('你好').toMatch('/你/')
expect('你好啊').toMatch('你好')
數(shù)組 array Set 相關(guān)
toContain 是否包含 也可以用到字符串上
const arr = [1, 2, 3, 4]
expect(arr).toContain(3)
const setData = new Set(arr)
expect(setData).toContain(2)
異常相關(guān)的
toThrow(拋出的異常信息 "可以不填愿待,可以寫正則") 測試異常
const errorFn = () => {
throw new Error('這是一個錯誤')
}
test('測試函拋出出異常', () => {
expect(errorFn).toThrow() // 正確
expect(errorFn).toThrow('這是一個錯誤') // 正確
expect(errorFn).toThrow(/這是一個錯誤/) // 正確
expect(errorFn).toThrow('這不是一個錯誤') // 錯誤
})