匹配器的使用

Jest使用“匹配器”的机制让你可以使用各种方法进行测试 这篇文档将向你介绍一些常用的匹配器, 想要看到完整的列表,请查阅 expect API 的文档

常用的匹配器

最简单测试一个值的方法是使用精确匹配的方法。

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

在上面的代码中,expect (2 + 2) 返回了一个”预期”的对象。 你通常不会对这些期望对象调用过多的匹配器。 在此代码中,.toBe(4) 是匹配器。 当 Jest 运行时,它会跟踪所有失败的匹配器,以便它可以为你打印出很好的错误消息。

toBe使用 Object.is来进行精准匹配的测试。 如果您想要检查对象的值,请使用 toEqual 代替:

  1. test('对象赋值', () => {
  2. const data = {one: 1};
  3. data['two'] = 2;
  4. expect(data).toEqual({one: 1, two: 2});
  5. });

toEqual 递归检查对象或数组的每个字段。

您还可以测试相反的匹配︰

  1. test('adding positive numbers is not zero', () => {
  2. for (let a = 1; a < 10; a++) {
  3. for (let b = 1; b < 10; b++) {
  4. expect(a + b).not.toBe(0);
  5. }
  6. }
  7. });

真值

代码中的undefined, null, and false有不同含义,若你在测试时不想区分他们,可以用真值判断。 Jest提供helpers供你使用。

  • toBeNull 只匹配 null
  • toBeUndefined 只匹配 undefined
  • toBeDefinedtoBeUndefined 相反
  • toBeTruthy 匹配任何 if 语句为真
  • toBeFalsy 匹配任何 if 语句为假

例如:

  1. test('null', () => {
  2. const n = null;
  3. expect(n).toBeNull();
  4. expect(n).toBeDefined();
  5. expect(n).not.toBeUndefined();
  6. expect(n).not.toBeTruthy();
  7. expect(n).toBeFalsy();
  8. });
  9. test('zero', () => {
  10. const z = 0;
  11. expect(z).not.toBeNull();
  12. expect(z).toBeDefined();
  13. expect(z).not.toBeUndefined();
  14. expect(z).not.toBeTruthy();
  15. expect(z).toBeFalsy();
  16. });

你应该使用最精确匹配的匹配器来测试你期望你的代码能干什么。

数字

大多数的比较数字有等价的匹配器。

  1. test('two plus two', () => {
  2. const value = 2 + 2;
  3. expect(value).toBeGreaterThan(3);
  4. expect(value).toBeGreaterThanOrEqual(3.5);
  5. expect(value).toBeLessThan(5);
  6. expect(value).toBeLessThanOrEqual(4.5);
  7. // toBe and toEqual are equivalent for numbers
  8. expect(value).toBe(4);
  9. expect(value).toEqual(4);
  10. });

对于比较浮点数相等,使用 toBeCloseTo 而不是 toEqual,因为你不希望测试取决于一个小小的舍入误差。

  1. test('两个浮点数字相加', () => {
  2. const value = 0.1 + 0.2;
  3. //expect(value).toBe(0.3); 这句会报错,因为浮点数有舍入误差
  4. expect(value).toBeCloseTo(0.3); // 这句可以运行
  5. });
  6. });

字符串

您可以检查对具有 toMatch 正则表达式的字符串︰

  1. test('there is no I in team', () => {
  2. expect('team').not.toMatch(/I/);
  3. });
  4. test('but there is a "stop" in Christoph', () => {
  5. expect('Christoph').toMatch(/stop/);
  6. });

数组和可迭代对象

你可以通过 toContain来检查一个数组或可迭代对象是否包含某个特定项:

  1. const shoppingList = [
  2. 'diapers',
  3. 'kleenex',
  4. 'trash bags',
  5. 'paper towels',
  6. 'milk',
  7. ];
  8. test('shoppingList数组中包含milk', () => {
  9. expect(shoppingList).toContain('milk');
  10. expect(new Set(shoppingList)).toContain('milk');
  11. });

例外

若你想测试某函数在调用时是否抛出了错误,你需要使用 toThrow

  1. function compileAndroidCode() {
  2. throw new Error('you are using the wrong JDK');
  3. }
  4. test('compiling android goes as expected', () => {
  5. expect(() => compileAndroidCode()).toThrow();
  6. expect(() => compileAndroidCode()).toThrow(Error);
  7. // 你可以自己定义确切的错误消息内容或者使用正则表达式
  8. expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
  9. expect(() => compileAndroidCode()).toThrow(/JDK/);
  10. });

注意:抛出错误的函数需要在expect的包装函数中调用,否则 toThrow断言总是会失败(不会捕获到前面的错误)。

以及更多

这些只是浅尝辄止。 匹配器的完整列表,请查阅 参考文档

一旦你学会了如何使用匹配器后,接下来可以学习 Jest 是如何让你可以 测试异步代码的。