Using Matchers (ok)

https://jestjs.io/docs/using-matchers

Common Matchers

The simplest way to test a value is with exact equality.

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

Trong mã này, kỳ vọng (2 + 2) trả về một đối tượng "kỳ vọng". Bạn thường sẽ không làm được gì nhiều với các đối tượng kỳ vọng này ngoại trừ các trình khớp lệnh gọi trên chúng. Trong mã này, .toBe (4) là trình đối sánh. Khi Jest chạy, nó sẽ theo dõi tất cả các trình khớp lỗi để có thể in ra các thông báo lỗi đẹp cho bạn.

toBe sử dụng Object.is để kiểm tra sự bình đẳng chính xác. Nếu bạn muốn kiểm tra giá trị của một đối tượng, hãy sử dụng toEqual để thay thế:

test('object assignment', () => {
  const data = {one: 1};
  data['two'] = 2;
  expect(data).toEqual({one: 1, two: 2});
});

toEqual kiểm tra đệ quy mọi trường của một đối tượng hoặc mảng.

test('object assignment', () => {
  const data = {one: 1};
  data['two'] = 2;
  expect(data).toEqual({one: 1, two: 2});
});

You can also test for the opposite of a matcher:

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

Truthiness

Trong các bài kiểm tra, đôi khi bạn cần phân biệt giữa không xác định, null và sai, nhưng đôi khi bạn không muốn xử lý chúng theo cách khác nhau. Jest chứa những người trợ giúp cho phép bạn rõ ràng về những gì bạn muốn.

  • toBeNull matches only null

  • toBeUndefined matches only undefined

  • toBeDefined is the opposite of toBeUndefined

  • toBeTruthy matches anything that an if statement treats as true

  • toBeFalsy matches anything that an if statement treats as false

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

Numbers

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

For floating point equality, use toBeCloseTo instead of toEqual, because you don't want a test to depend on a tiny rounding error.

test('adding floating point numbers', () => {
  const value = 0.1 + 0.2;
  //expect(value).toBe(0.3);           This won't work because of rounding error
  expect(value).toBeCloseTo(0.3); // This works.
});

Strings

test('there is no I in team', () => {
  expect('team').not.toMatch(/I/);
});

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

Arrays and iterables

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

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

Exceptions

Lưu ý: hàm ném một ngoại lệ cần phải được gọi trong một hàm gói, nếu không, khẳng định toThrow sẽ không thành công.

function compileAndroidCode() {
    throw new Error('you are using the wrong JDK');
}

test('compiling android goes as expected', () => {
    expect(() => compileAndroidCode()).toThrow();
    expect(() => compileAndroidCode()).toThrow(Error);
    // You can also use the exact error message or a regexp
    expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
    expect(() => compileAndroidCode()).toThrow(/JDK/);
});

Last updated