前端自动化测试框架Jest中的匹配器

发表于:2022-12-12 09:31

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:三年没洗澡    来源:掘金

#
Jest
  众所周知,在前面的代码中,我们在编写一个测试用例的时候用到了 test、expect、toBe 三个方法。
  这种语法大概可以翻译为:期待一个值的结果去匹配另一个值。
  比如:
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 new Error("this is a new err");
};
test("测试 fn1、fn2 调用时是否有异常", () => {
  expect(fn1).toThrow(); // 不通过
  expect(fn2).toThrow(); // 通过
});
  在 Jest 中,除了上面这些列举出来的常用的匹配器之外,还有很多匹配器为我们提供。不需要记住所有,知道常用的几个即可。如果想了解更多,可以点击这里查看官方文档。
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号