众所周知,在前面的代码中,我们在编写一个测试用例的时候用到了 test、expect、toBe 三个方法,在之前我们也介绍了 Jest 的基础使用、Jest
的简单配置,今天我们来说说 Jest 中的匹配器(matchers)。

这种语法大概可以翻译为:期待一个值的结果去匹配另一个值。

比如:
expect(1 + 1).toBe(2);
这个例子可以翻译为:期待 1 + 1 的结果是 2,这里的 toBe 就是一个匹配器,用来判断接收值和期望值是否相等。

事实上,在 Jest 中,除了 toBe 之外,还有很多匹配器。

<>toBe

测试两个对象的值是否相等,类似于 js 中的 === 运算符。
// toBe test("测试加法交换律", () => { for (let x = 0; x <= 10; x++) { for (let y = 0;
y<= 10; y++) { expect(x + y).toBe(y + x); } } });
<>toEqual

测试两个对象的原始值是否相等,只检查内容,不检查引用。
// toEqual const can1 = { value: "hello" }; const can2 = { value: "hello" };
test("测试 can1 和 can2 的内容是否相等", () => { expect(can1).toEqual(can2); });
<>toBeNull

测试对象的值是否为null,效果相当于.toBe(null)。
// toBeNull const value = null; test("测试值是否为 null", () => { expect(value).
toBeNull(); });
<>toBeUndefined

测试对象的值是否为undefined,效果相当于.toBe(undefined)。
// toBeUndefined const value = undefined; test("测试值是否为 undefined", () => {
expect(value).toBeUndefined(); });
<>toBeDefined

测试值是否被定义过,除了undefined之外都会通过测试。
// toBeDefined const value = 1; test("测试值是否被定义过", () => { expect(value).
toBeDefined(); });
<>toBeTruthy

检查值转成布尔值之后是否为真值。

<>toBeFalsy

检查值转成布尔值之后是否为假值。
// toBeTruthy、toBeFalsy test("测试是否为真值", () => { expect(0).toBeTruthy(); // 不通过
expect("").toBeTruthy(); // 不通过 expect(null).toBeTruthy(); // 不通过 expect(false).
toBeTruthy(); // 不通过 expect(undefined).toBeTruthy(); // 不通过 }); test("测试是否为假值",
() => { expect(0).toBeFalsy(); // 通过 expect("").toBeFalsy(); // 通过 expect(null).
toBeFalsy(); // 通过 expect(false).toBeFalsy(); // 通过 expect(undefined).toBeFalsy(
); // 通过 });
<>not

取反匹配器,相当于 js 中的 ! 运算符。
// not test("测试值是否不为 aaa", () => { expect("hello").not.toBe("aaa"); }); test(
"测试值是否不为 null", () => { expect([]).not.toBeNull(); }); test("测试值是否不为 undefined",
() => { expect({}).not.toBeUndefined(); });
<>toBeGreaterThan

检查接收值是否大于期待值。
// toBeGreaterThan test("测试 10 是否大于 9", () => { expect(10).toBeGreaterThan(9);
});
<>toBeLessThan

检查接收值是否小于期待值。
// toBeLessThan test("测试 10 是否小于 20", () => { expect(10).toBeLessThan(20); });
toBeGreaterThanOrEqual

检查接收值是否大于等于期待值。
// toBeGreaterThanOrEqual test("测试 10 是否大于等于 10", () => { expect(10).
toBeGreaterThanOrEqual(10); });
<>toBeLessThanOrEqual

检查接收值是否小于等于期待值。
// toBeLessThanOrEqual test("测试 10 是否小于等于 10", () => { expect(10).
toBeLessThanOrEqual(10); });
<>toBeCloseTo

检查浮点数是否接近(是否近似相等)。
// toBeCloseTo test("测试 0.1 + 0.2 是否等于 0.3", () => { expect(0.1 + 0.2).toBe(0.3
); // 不通过 expect(0.1 + 0.2).toBeCloseTo(0.3); //通过 });
<>toMatch

检查值是否和字符串或者正则相匹配。
// toMatch test("测试字符串是否包含 baidu", () => { expect("www.baidu.com").toMatch(
"baidu"); expect("www.baidu.com").toMatch(/baidu/); });
<>toContain

检查数组中是否包含某一项(类似于 js 中的 includes 方法)。
// toContain test("测试 list 中是否包含 3", () => { const list = [1, 2, 3]; expect(
list).toContain(3); });
<>toThrow

测试函数在调用时是否有异常抛出。
// toThrow const fn1 = () => { console.log("hello"); }; const fn2 = () => {
throw newError("this is a new err"); }; test("测试 fn1、fn2 调用时是否有异常", () => {
expect(fn1).toThrow(); // 不通过 expect(fn2).toThrow(); // 通过 });
在 Jest
中,除了上面这些列举出来的常用的匹配器之外,还有很多匹配器为我们提供。不需要记住所有,知道常用的几个即可。如果想了解更多,可以点击这里查看官方文档。

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信