支持的Matchers
toBe&&toEqual- 真值,结果同if(/*statement*/)
- 数值,
toBeGreaterThan,toBeGreaterThanOrEqual,toBeLessThan,toBeLessThanOrEqual,toBe,toEqual - String,
toMatch - 数组和Iterables,
toContain - 异常,
toThrow
异步测试
- 回调,
done Promise,then里直接expect,catch需要加一条语句expect.assertions(1);.resolves/.rejectsAsync/Await,类似于Promise
describe
jest会先调用describe,因此一些有意义的逻辑应该放在beforeEach、afterEach里。jest会按照test定义的顺序来执行test。
1 | describe('1', () => { |
调试技巧
当某个test出错时,可以使用test.only来检查是它本身的错误,还是与其他test有关。
如果test.only时通过,和其他test一起就不通过,猜测很可能与共享状态有关。可以在beforeEach里重置共享状态来验证。
1 | test.only(() => { |