返回

易混淆的Jest验证函数使用详解

前端

前言

在Jest单元测试中,验证函数是用来检查实际值是否符合预期值。它们通常与expect函数一起使用,可方便地验证变量是否满足特定条件。Jest提供了一系列验证函数,包括toBe、toEqual、toBeNull、toBeUndefined、toBeTruthy、toBeFalsy、toBeGreaterThan、toBeLessThan、toHaveLength、toThrow和toBeCloseTo等。

这些验证函数虽然用途相似,但在某些情况下可能会让人感到混淆。为了帮助您更好地理解和使用这些验证函数,本文将对它们进行详细介绍,并提供实际示例帮助您理解它们的用法。

常用验证函数详解

1. toBe

toBe用于比较两个值是否严格相等。这意味着两个值必须是相同类型,并且具有相同的值。例如:

expect(1).toBe(1); // 通过
expect('foo').toBe('foo'); // 通过
expect(true).toBe(true); // 通过
expect(null).toBe(null); // 通过
expect(undefined).toBe(undefined); // 通过

需要注意的是,toBe对于引用类型(如对象、数组)的比较是通过比较它们的内存地址来进行的,而不是比较它们的内容。因此,对于引用类型,我们通常使用toEqual进行比较。

2. toEqual

toEqual用于比较两个值是否相等。相对于toBe,toEqual对于引用类型(如对象、数组)的比较是通过比较它们的内容来进行的,而不是比较它们的内存地址。例如:

expect({ foo: 'bar' }).toEqual({ foo: 'bar' }); // 通过
expect([1, 2, 3]).toEqual([1, 2, 3]); // 通过
expect({ foo: 'bar' }).toEqual({ bar: 'foo' }); // 失败
expect([1, 2, 3]).toEqual([1, 3, 2]); // 失败

3. toBeNull

toBeNull用于检查一个值是否为null。例如:

expect(null).toBeNull(); // 通过
expect(undefined).toBeNull(); // 失败
expect(0).toBeNull(); // 失败

4. toBeUndefined

toBeUndefined用于检查一个值是否为undefined。例如:

expect(undefined).toBeUndefined(); // 通过
expect(null).toBeUndefined(); // 失败
expect(0).toBeUndefined(); // 失败

5. toBeTruthy

toBeTruthy用于检查一个值是否为真值。真值包括以下值:

  • true
  • 任何非0数字
  • 任何非空字符串
  • 任何非null对象

例如:

expect(true).toBeTruthy(); // 通过
expect(1).toBeTruthy(); // 通过
expect('foo').toBeTruthy(); // 通过
expect({}).toBeTruthy(); // 通过
expect(false).toBeTruthy(); // 失败
expect(0).toBeTruthy(); // 失败
expect('').toBeTruthy(); // 失败
expect(null).toBeTruthy(); // 失败

6. toBeFalsy

toBeFalsy用于检查一个值是否为假值。假值包括以下值:

  • false
  • 0
  • 空字符串
  • null
  • undefined

例如:

expect(false).toBeFalsy(); // 通过
expect(0).toBeFalsy(); // 通过
expect('').toBeFalsy(); // 通过
expect(null).toBeFalsy(); // 通过
expect(undefined).toBeFalsy(); // 通过
expect(true).toBeFalsy(); // 失败
expect(1).toBeFalsy(); // 失败
expect('foo').toBeFalsy(); // 失败
expect({}).toBeFalsy(); // 失败

7. toBeGreaterThan

toBeGreaterThan用于检查一个值是否大于另一个值。例如:

expect(1).toBeGreaterThan(0); // 通过
expect('b').toBeGreaterThan('a'); // 通过
expect(10.5).toBeGreaterThan(10); // 通过
expect(0).toBeGreaterThan(-1); // 失败
expect('a').toBeGreaterThan('b'); // 失败
expect(10).toBeGreaterThan(10.5); // 失败

8. toBeLessThan

toBeLessThan用于检查一个值是否小于另一个值。例如:

expect(0).toBeLessThan(1); // 通过
expect('a').toBeLessThan('b'); // 通过
expect(10).toBeLessThan(10.5); // 通过
expect(1).toBeLessThan(0); // 失败
expect('b').toBeLessThan('a'); // 失败
expect(10.5).toBeLessThan(10); // 失败

9. toHaveLength

toHaveLength用于检查一个字符串、数组或对象是否具有指定长度。例如:

expect('foo').toHaveLength(3); // 通过
expect([1, 2, 3]).toHaveLength(3); // 通过
expect({ foo: 'bar' }).toHaveLength(1); // 通过
expect('foo').toHaveLength(4); // 失败
expect([1, 2, 3]).toHaveLength(2); // 失败
expect({ foo: 'bar' }).toHaveLength(2); // 失败

10. toThrow

toThrow用于检查一个函数是否抛出一个错误。例如:

function add(a, b) {
  if (a < 0 || b < 0) {
    throw new Error('Arguments must be non-negative');
  }

  return a + b;
}

expect(() => add(-1, 2)).toThrow(); // 通过
expect(() => add(1, -2)).toThrow(); // 通过
expect(() => add(1, 2)).toThrow(); // 失败

11. toBeCloseTo

toBeCloseTo用于检查一个值是否接近另一个值。例如:

expect(1.1).toBeCloseTo(1.2, 0.1); // 通过
expect(0.1 + 0.2).toBeCloseTo(0.3, 0.01); // 通过
expect(1.1).toBeCloseTo(1.2, 0.05); // 失败
expect(0.1 + 0.2).toBeCloseTo(0.3, 0.005); // 失败

结语

以上就是Jest中一些常用验证函数的详细介绍。希望通过本文的讲解,您能够对这些验证函数有更深入的了解,并在实际工作中熟练地使用它们。