0
点赞
收藏
分享

微信扫一扫

Jest测试语法系列之Matchers


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

相等匹配

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

test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});

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

Jest测试语法系列之Matchers_返回结果

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

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的;

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();
});

数字型匹配

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

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 ,因为避免细微的四舍五入引起额外的问题。例如:

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 匹配规则,支持正则表达式匹配。例如:

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 检查是否包含。例如:

const shoppingList = [
'diapers',
'kleenex',
'trash bags',
'paper towels',
'beer',
];

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

异常匹配

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

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/);
});


举报

相关推荐

0 条评论