1.使用babel
下载babel相关的包并配置好babelrc文件即可,jest会自动使用babel
2.匹配器
toBe 可以理解为Object.is() //与===不同的是 +0 != -0 NaN = NaN
toEqual 只比较内容和类型
toBeNull 是否等于null,undefined也不等于null
toBeUndefined 等否等于undefined ''也不等于undefined
toBeDefined 只要不是undefined就行
toBeTruthy 是否为真值 1 true “abc”
toBeFalsy 是否为假值 undefined null
not 取反 eg: expect('1').not.toBeFalsy() 该测试用例可以通过测试
toBeGreaterThan(10) 大于10
toBeLessThan(10) 小于10
toBeGreaterThanOrEqual(10) 大于等于10
toBeCloseTo(0.3) 用于小数比较
toMatch('ab') 字符串是包含“ab”,也可以使用正则
toContain('a') 数组是否包含‘a’
toThrow() 是否抛出错误
function throwErrorFun(){
throw new Error()
}
expect(throwErroeFun).toThrow()
function throwError() {
throw new Error('this is err')
}
test('测试报错', () => {
expect(throwError).toThrow('this is err')
})
jest匹配器笔记(非教程)
最新推荐文章于 2023-02-13 11:30:52 发布