前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jest测试语法系列之Matchers

Jest测试语法系列之Matchers

作者头像
xiangzhihong
发布2022-11-30 14:55:20
5340
发布2022-11-30 14:55:20
举报
文章被收录于专栏:向治洪

关于Jest测试的基础内容,可以参考之前的博客:前端单元测试之Jest 本文主要讲的是匹配器(Matchers),匹配器(Matchers)是Jest中非常重要的一个概念,它可以提供很多种方式来让你去验证你所测试的返回值,本文重点介绍几种常用的Matcher,其他的可以通过官网api文档查看。

相等匹配

例如,有下面一段测试代码:

代码语言:javascript
复制
test('two plus two is four', () => {
  expect(2 + 2).toBe(4);
});

在这段代码中 expact(2 + 2) 将返回我们期望的结果,通常情况下我们只需要调用expect就可以,括号中的可以是一个具有返回值的函数,也可以是表达式。后面的 toBe 就是一个matcher,当Jest运行的时候它会记录所有失败的matcher的详细信息并且输出给用户,让维护者清楚的知道failed的原因,如果我们改成 toBe(5),将会输出错误的提示,如下图:

在这里插入图片描述
在这里插入图片描述

toBe 是测试具体的某一个值,如果需要测试对象,需要用到toEqual。例如:

代码语言:javascript
复制
test('object assignment', () => {
  const data = {one: 1};
  data['two'] = 2;
  expect(data).toEqual({one: 1, two: 2});
});

真实性匹配

在实际项目测试中,有时需要区分undefined、null和false,这些可以使用Jest的真实性匹配。

  • toBeNull 仅当expect返回对象为 null时;
  • toBeUndefined 仅当返回为 undefined;
  • toBeDefined 和上面的刚好相反,对象如果有定义时;
  • toBeTruthy 匹配任何返回结果为true的;
  • toBeFalsy 匹配任何返回结果为false的;
代码语言:javascript
复制
test('null', () => {
  const n = null;
  expect(n).toBeNull();
  expect(n).toBeDefined();
  expect(n).not.toBeUndefined();
  expect(n).not.toBeTruthy();
  expect(n).toBeFalsy();
});


test('two plus two', () => {
  const value = 2 + 2;
  expect(value).toBeGreaterThan(3);
  expect(value).toBeGreaterThanOrEqual(3.5);
  expect(value).toBeLessThan(5);
  expect(value).toBeLessThanOrEqual(4.5);

  // toBe and toEqual are equivalent for numbers
  expect(value).toBe(4);
  expect(value).toEqual(4);
});

test('zero', () => {
    const z = 0;
    expect(z).not.toBeNull();
    expect(z).toBeDefined();
    expect(z).not.toBeUndefined();
    expect(z).not.toBeTruthy();
    expect(z).toBeFalsy();
});

数字型匹配

数字型匹配规则非常语义化,不需要解释都能看得懂。例如:

代码语言:javascript
复制
test('two plus two', () => {
  const value = 2 + 2;
  expect(value).toBeGreaterThan(3);
  expect(value).toBeGreaterThanOrEqual(3.5);
  expect(value).toBeLessThan(5);
  expect(value).toBeLessThanOrEqual(4.5);

  // toBe and toEqual are equivalent for numbers
  expect(value).toBe(4);
  expect(value).toEqual(4);
});

需要注意的是对于float类型的浮点数计算的时候,需要使用toBeCloseTo而不是 toEqual ,因为避免细微的四舍五入引起额外的问题。例如:

代码语言:javascript
复制
test('adding floating point numbers', () => {
  const value = 0.1 + 0.2;
  //expect(value).toBe(0.3);           错误
  expect(value).toBeCloseTo(0.3); // This works.
});

关于0.1 + 0.2 为什么不等于 0.3 ,大家可以查看如下文档:http://u3xyz.com/detail/28

字符型匹配

使用 toMatch 匹配规则,支持正则表达式匹配。例如:

代码语言:javascript
复制
test('there is no I in team', () => {
  expect('team').not.toMatch(/I/);
});

test('but there is a "stop" in Christoph', () => {
  expect('Christoph').toMatch(/stop/);
});

数组类型匹配

使用 toContain 检查是否包含。例如:

代码语言:javascript
复制
const shoppingList = [
  'diapers',
  'kleenex',
  'trash bags',
  'paper towels',
  'beer',
];

test('the shopping list has beer on it', () => {
  expect(shoppingList).toContain('beer');
}); 

异常匹配

如果想要测试function是否会抛出特定的异常信息,可以用 toThrow 。例如:

代码语言:javascript
复制
function compileAndroidCode() {
  throw new ConfigError('you are using the wrong JDK');
}

test('compiling android goes as expected', () => {
  expect(compileAndroidCode).toThrow();
  expect(compileAndroidCode).toThrow(ConfigError);

  // You can also use the exact error message or a regexp
  expect(compileAndroidCode).toThrow('you are using the wrong JDK');
  expect(compileAndroidCode).toThrow(/JDK/);
});
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-11-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 相等匹配
  • 真实性匹配
  • 数字型匹配
  • 字符型匹配
  • 数组类型匹配
  • 异常匹配
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档