预计·续5

test('ounces per can is at least 12', () => {  expect(ouncesPerCan()).toBeGreaterThanOrEqual(12);});

.toBeLessThan(number | bigint)

Use toBeLessThan to compare received < expected for number or big integer values. Use toBeLessThan to compare received < expected for number or big integer values. For example, test that ouncesPerCan() returns a value of less than 20 ounces:

test('ounces per can is less than 20', () => {  expect(ouncesPerCan()).toBeLessThan(20);});

.toBeLessThanOrEqual(number | bigint)

Use toBeLessThanOrEqual to compare received <= expected for number or big integer values. For example, test that ouncesPerCan() returns a value of at most 12 ounces: For example, test that ouncesPerCan() returns a value of at most 12 ounces:

test('ounces per can is at most 12', () => {  expect(ouncesPerCan()).toBeLessThanOrEqual(12);});

.toBeInstanceOf(Class)

Use .toBeInstanceOf(Class) to check that an object is an instance of a class. This matcher uses instanceof underneath. This matcher uses instanceof underneath.

class A {}expect(new A()).toBeInstanceOf(A);expect(() => {}).toBeInstanceOf(Function);expect(new A()).toBeInstanceOf(Function); // throws

.toBeNull()

.toBeNull() is the same as .toBe(null) but the error messages are a bit nicer. So use .toBeNull() when you want to check that something is null. So use .toBeNull() when you want to check that something is null.

function bloop() {  return null;}test('bloop returns null', () => {  expect(bloop()).toBeNull();});

.toBeTruthy()

Use .toBeTruthy when you don’t care what a value is and you want to ensure a value is true in a boolean context. For example, let’s say you have some application code that looks like: For example, let’s say you have some application code that looks like:

drinkSomeLaCroix();if (thirstInfo()) {  drinkMoreLaCroix();}

You may not care what thirstInfo returns, specifically - it might return true or a complex object, and your code would still work. So if you want to test that thirstInfo will be truthy after drinking some La Croix, you could write: So if you want to test that thirstInfo will be truthy after drinking some La Croix, you could write:

test('drinking La Croix leads to having thirst info', () => {  drinkSomeLaCroix();  expect(thirstInfo()).toBeTruthy();});

In JavaScript, there are six falsy values: false, 0, '', null, undefined, and NaN. Everything else is truthy. Everything else is truthy.

.toBeUndefined()

Use .toBeUndefined to check that a variable is undefined. Use .toBeUndefined to check that a variable is undefined. For example, if you want to check that a function bestDrinkForFlavor(flavor) returns undefined for the 'octopus' flavor, because there is no good octopus-flavored drink:

test('the best drink for octopus flavor is undefined', () => {  expect(bestDrinkForFlavor('octopus')).toBeUndefined();});

You could write expect(bestDrinkForFlavor('octopus')).toBe(undefined), but it’s better practice to avoid referring to undefined directly in your code.

.toBeNaN()

Use .toBeNaN when checking a value is NaN.

test('passes when value is NaN', () => {  expect(NaN).toBeNaN();  expect(1).not.toBeNaN();});

.toContain(item)

Use .toContain when you want to check that an item is in an array. For testing the items in the array, this uses ===, a strict equality check. .toContain can also check whether a string is a substring of another string. For testing the items in the array, this uses ===, a strict equality check. .toContain can also check whether a string is a substring of another string.

For example, if getAllFlavors() returns an array of flavors and you want to be sure that lime is in there, you can write:

test('the flavor list contains lime', () => {  expect(getAllFlavors()).toContain('lime');});

.toContainEqual(item)

Use .toContainEqual when you want to check that an item with a specific structure and values is contained in an array. For testing the items in the array, this matcher recursively checks the equality of all fields, rather than checking for object identity. For testing the items in the array, this matcher recursively checks the equality of all fields, rather than checking for object identity.

describe('my beverage', () => {  test('is delicious and not sour', () => {    const myBeverage = {delicious: true, sour: false};    expect(myBeverages()).toContainEqual(myBeverage);  });});

.toEqual(value)

Use .toEqual to compare recursively all properties of object instances (also known as “deep” equality). It calls Object.is to compare primitive values, which is even better for testing than === strict equality operator. Use .toBe to compare primitive values or to check referential identity of object instances. It calls Object.is to compare values, which is even better for testing than === strict equality operator.

For example, .toEqual and .toBe behave differently in this test suite, so all the tests pass:

const can1 = {  flavor: 'grapefruit',  ounces: 12,};const can2 = {  flavor: 'grapefruit',  ounces: 12,};describe('the La Croix cans on my desk', () => {  test('have all the same properties', () => {    expect(can1).toEqual(can2);  });  test('are not the exact same can', () => {    expect(can1).not.toBe(can2);  });});

Note: .toEqual won’t perform a deep equality check for two errors. Only the message property of an Error is considered for equality. It is recommended to use the .toThrow matcher for testing against errors. Only the message property of an Error is considered for equality. It is recommended to use the .toThrow matcher for testing against errors.

If differences between properties do not help you to understand why a test fails, especially if the report is large, then you might move the comparison into the expect function. For example, use equals method of Buffer class to assert whether or not buffers contain the same content: For example, use equals method of Buffer class to assert whether or not buffers contain the same content:

  • rewrite expect(received).toEqual(expected) as expect(received.equals(expected)).toBe(true)

  • rewrite expect(received).not.toEqual(expected) as expect(received.equals(expected)).toBe(false)

.toMatch(regexp | string)

Use .toMatch to check that a string matches a regular expression.

For example, you might not know what exactly essayOnTheBestFlavor() returns, but you know it’s a really long string, and the substring grapefruit should be in there somewhere. You can test this with: You can test this with:

describe('an essay on the best flavor', () => {  test('mentions grapefruit', () => {    expect(essayOnTheBestFlavor()).toMatch(/grapefruit/);    expect(essayOnTheBestFlavor()).toMatch(new RegExp('grapefruit'));  });});

This matcher also accepts a string, which it will try to match:

describe('grapefruits are healthy', () => {  test('grapefruits are a fruit', () => {    expect('grapefruits').toMatch('fruit');  });});

.toMatchObject(object)

Use .toMatchObject to check that a JavaScript object matches a subset of the properties of an object. It will match received objects with properties that are not in the expected object. It will match received objects with properties that are not in the expected object.

You can also pass an array of objects, in which case the method will return true only if each object in the received array matches (in the toMatchObject sense described above) the corresponding object in the expected array. This is useful if you want to check that two arrays match in their number of elements, as opposed to arrayContaining, which allows for extra elements in the received array. This is useful if you want to check that two arrays match in their number of elements, as opposed to arrayContaining, which allows for extra elements in the received array.

You can match properties against values or against matchers.