Jest的匹配器

toBe 匹配器

toBe 匹配器有种类似于object.is或者===,精确相等。

test('测试toBe', () => {
  expect(10).toBe(10); // passed
});


test('测试toBe', () => {
  const a = {one: 1}
  expect(a).toBe( {one: 1});  // failed,因为两个对象的地址是不一样的
});

 

toEqual匹配器

测试对象的内容是否相等,不比较对象的地址,只关心对象的内容是否一致,递归检查对象或数组的每个字段。

test('测试toEqual', () => {
  const a = {one: 1}
  expect(a).toEqual( {one: 1});  // passed
});

 

toBeNull匹配器

测试某个变量是否为null,如果是则Passed,否则failed

test('测试toBeNull', () => {
  const a = null
  expect(a).toBeNull();  // passed
});

 

toBeUndefined匹配器和toBeDefined匹配器

测试某个变量是否未定义,如果是则Passed,否则failed

test('测试toBeUndefined', () => {
  const a = undefined;
  expect(a).toBeUndefined();  // passed
});

test('测试toBeUndefined', () => {
  const a = '';
  expect(a).toBeUndefined();  // failed
});

test('测试toBeUndefined', () => {
  const a = null;
  expect(a).toBeUndefined();  // failed
});
test('测试toBeDefined', () => {
  const a = null;
  expect(a).toBeDefined();  // passed
});

test('测试toBeDefined', () => {
  const a = undefined;
  expect(a).toBeDefined();  // failed
});

 

toBeTruthy匹配器

测试某个变量是否为真,如果是则Passed,否则failed

test('测试toBeTruthy', () => {
  const a = undefined;
  expect(a).toBeTruthy();  // undefined 视为false
});

test('测试toBeTruthy', () => {
  const a = null;
  expect(a).toBeTruthy();  // null视为false
});

test('测试toBeTruthy', () => {
  const a = 0;
  expect(a).toBeTruthy();  // 0 视为false
});

test('测试toBeTruthy', () => {
  const a = 1;
  expect(a).toBeTruthy();  // 1 视为true
});

 

toBeFalsy匹配器

测试某个变量是否为假,如果是则Passed,否则failed

test('测试toBeFalsy', () => {
  const a = 1;
  expect(a).toBeFalsy();  // failed,因为1 视为true
});

test('测试toBeFalsy', () => {
  const a = undefined;
  expect(a).toBeFalsy();  // passed,因为undefined 视为false
});

test('测试toBeFalsy', () => {
  const a = null;
  expect(a).toBeFalsy();  // passed,因为null 视为false
});

test('测试toBeFalsy', () => {
  const a = 0;
  expect(a).toBeFalsy();  // passed,因为0 视为false
});
test('测试toBeFalsy', () => {
  const a = 0;
  expect(a).not.toBeFalsy();  // failed,因为0 视为false,但是匹配器要的是真
});

 

数字相关的匹配器

test('测试toBeGreaterThan', () => {
  const count = 10;
  expect(count).toBeGreaterThan(9);  // passed,表示希望count这个变量的值比9大
});

test('测试toBeLessThan', () => {
  const count = 10;
  expect(count).toBeLessThan(9);  // failed,表示希望count这个变量的值比9小
});

test('测试toBeGreaterThanOrEqual', () => {
  const count = 9;
  expect(count).toBeGreaterThanOrEqual(9);   // passed,表示希望count这个变量的值大于等于9
});

test('测试toBeLessThanOrEqual', () => {
  const count = 9;
  expect(count).toBeLessThanOrEqual(9);   // passed,表示希望count这个变量的值小于等于9
});


test('测试toBeCloseTo', () => {
  const firstNumber = 0.1;
  const secondNumber = 0.2;
  expect(firstNumber + secondNumber).toEqual(0.3);  // 结果是failed,因为js计算浮点数的时
  expect(value).toBe(0.3);          //  这句会报错,因为浮点数有舍入误差候,有可能会溢出或者说不准确,这种情况下最好用toBeCloseTo
});

test('测试toBeCloseTo', () => {
  const firstNumber = 0.3;
  const secondNumber = 0.4;
  expect(firstNumber + secondNumber).toBeCloseTo(0.7);   // passed
});

 

字符串相关的匹配器

test('测试toMatch', () => {
  const str = 'www.baidu.com';
  expect(str).toMatch('baidu');   // passed, 表示str字符串中是否包含baidu这个字符串,是返回passed
  expect(str).toMatch(/baidu/); //passed,这里还可以写正则表达式
});

 

数组相关的匹配器

test('测试toContain', () => {
  const arr = ['dee', 'lee'];
  expect(arr).toContain('dee');   // passed, 表示arr数组中是否包含dee这个字符串元素,是返回passed
});

test('测试toContain', () => {
  const arr = ['dee', 'lee'];
  const data = new Set(arr);
  expect(data).toContain('dee');   // passed, 表示arr数组中是否包含dee这个字符串元素,是返回passed
});

 

异常情况的匹配器

const throwNewErrorFunc =  () => {
  throw new Error('this is a new error');
}

test('测试toThrow', () => {
  expect(throwNewErrorFunc).toThrow();   // passed, 表示希望throwNewErrorFunc这个方法运行的时候能够抛出一个异常
});

test('测试toThrow', () => {
  expect(throwNewErrorFunc).not.toThrow();   // failed, 表示希望throwNewErrorFunc这个方法运行的时候不能够抛出异常
});

test('测试toThrow', () => {
  expect(throwNewErrorFunc).toThrow('this is a new error');   // passed, 表示希望throwNewErrorFunc这个方法运行的时候能够抛出一个异常,并且内容是'this is a new error'
  expect(throwNewErrorFunc).toThrow(/this is a new error/); // 也可以是正则表达式
});

 

更多的匹配器可以查看Jest官网:https://jestjs.io/docs/zh-Hans/using-matchers

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值