3. 匹配器
1. 判断相等:
注意:toBe 使用 Object.is 来测试精确相等。 如果想要检查对象的值,请使用toEqual代替,它会递归判断对象的每一个字段。对数值来说,toBe和toEqual都可以使用。
类比js值类型和引用类型。
test('2加2等于4', () => { expect(2+2).toBe(4); }); // 测试对象相等 test('测试对象的值', () => { const data = {a: 1}; expect(data).toEqual({a: 1}); }); // 测试不等,相反的值 test('2加2不等于1', () => { expect(2 + 2).not.toBe(1); }); |
2. 判断真假、空值:
toBeNull 只匹配 null;
toBeUndefined 只匹配 undefined;
toBeDefined 与 toBeUndefined 相反;
toBeTruthy 匹配任何 if 语句为真;
toBeFalsy 匹配任何 if 语句为假。
test('null', () => { const n = null; expect(n).toBeNull(); expect(n).toBeDefined(); expect(n).not.toBeUndefined(); expect(n).not.toBeTruthy(); expect(n).toBeFalsy(); }); |
3. 判断数字:
test('2加2', () => { const value = 2 + 2; expect(value).toBeGreaterThan(3); // 大于3 expect(value).toBeGreaterThanOrEqual(4); // 大于或等于4 expect(value).toBeLessThan(5); // 小于5 expect(value).toBeLessThanOrEqual(4.5); // 小于或等于4.5 }); |
4. 判断符点数:
可使用toBeCloseTo来解决JS浮点精度带来的问题。
test('两个浮点数字相加', () => { const value = 0.1 + 0.2; //expect(value).toBe(0.3); 这句会报错,因为浮点数有舍入误差 expect(value).toBeCloseTo(0.3); // 这句可以运行 }); |
5. 判断字符串:toMatch()
test('there is no I in team', () => { expect('team').not.toMatch(/I/); }); test('but there is a "stop" in Christoph', () => { expect('Christoph').toMatch(/stop/); }); |
6. 判断数组或可迭代的对象:toContain()
const shoppingList = [ 'diapers', 'kleenex', 'beer', ]; test('the shopping list has beer on it', () => { expect(shoppingList).toContain('beer'); }); |
7. 判断异常:toThrow()
function compileAndroidCode() { throw new Error('这是一个错误消息'); } test('compiling android goes as expected', () => { expect(compileAndroidCode).toThrow(); // 可以匹配异常消息的内容,也可以用正则来匹配内容 expect(compileAndroidCode).toThrow('这是一个错误消息'); expect(compileAndroidCode).toThrow(/消息/); }); |
Tips: 代码提示!
expect 的工具方法太多记不住怎么办,在 ts 开发环境下,安装 jest 和 @types/jest 包含的声明文件,通过声明文件可以获得jest的类型定义,也可以使用类型检查等特性,写代码的时候就会有提示。
npm i jest @types/jest |
ps: ts是不是很香,提高了打工人的生产力。
4. 异步测试
异步代码的测试,关键点在于告知测试框架测试何时完成,让其在恰当的时机进行断言。
1. Callbacks回调函数:done
当我们的test函数中出现了异步回调函数时,可以给test函数传入一个 done 参数,它是一个函数类型的参数。如果test函数传入了done,jest就会等到 done 被调用才会结束当前的test case,如果 done 没有被调用,则该test自动不通过测试。
it('Test async code with done', (done) => { setTimeout(() => { // expect something done(); }, 500) }); |
2. 返回Promise:expect.assertions(number)
assertions 断言
如果使用的是 promise,需要在测试中 返回 一个 promise,Jest 会自动等待 promise 被解析处理,如果 promise 被拒绝,那么测试失败。
例1:
test("Test async code with promise", () => { // 一个rejected状态的 Promise 不会让测试失败 expect.assertions(1); return doAsync().then((data) => { expect(data).toBe('example'); }); }); test("Test promise with an error", () => { // 一个fulfilled状态的 Promise 不会让测试失败 expect.assertions(1); return doAsync().catch(e => { expect(e).toMatch('error') }); }); |
注意1:确保 返回promise,如果忽略掉 return,那么测试会在 promise 完成之前完成。
注意2:expect.assertions(number)验证在测试期间是否调用了一定数量的断言。
同时满足以上两个条件。
函数 doAsync,该函数接收两个回调 callback1 和 callback2,它将以未知顺序异步调用这两个回调。
例2:
test("doAsync calls both callbacks", () => { expect.assertions(2); function callback1(data) { expect(data).toBeTruthy(); } function callback2(data) { expect(data).toBeTruthy(); } doAsync(callback1, callback2); }); |
使用expect.assertions(2)确保两个回调都实际被调用。
.resolves / .rejects Jest语法糖
例1中的代码用匹配符 resolves/rejects (这里有s,非Promise)可以改写为:
// 假设 doAsync() 返回一个promise,resolve的结果为字符串'example' it('Test async code with promise', () => { expect.assertions(1); return expect(doAsync()).resolves.toBe('example'); }); }); it('Test promise with an error', () => { expect.assertions(1); return expect(doAsync()).rejects.toMatch('error')); }); |
async/await Promise语法糖
实际开发中,我们更常用的是用async/await来开发业务代码,上面例子的也可以async/await实现。
// 假设 doAsync() 返回一个promise,resolve的结果为字符串'example' it('Test async code with promise', async () => { expect.assertions(1); const data = await doAsync(); expect(data).toBe('example'); }); }); |
async/await 也可以和 resolves/rejects 一起使用:
// 假设 doAsync() 返回一个promise,resolve的结果为字符串'example' it('Test async code with promise', async () => { expect.assertions(1); await expect(doAsync()).resolves.toBe('example'); }); }); |
3. done 和 assertions区别
done:异步回调确保测试;
assertions:Promise确保测试;
一般测试的时候,异步都是模拟mock出来的,要自己控制结束,而不是真正的异步。所以expect.assertions某些情况下无法替代done。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理