预计·续6

const houseForSale = {  bath: true,  bedrooms: 4,  kitchen: {    amenities: ['oven', 'stove', 'washer'],    area: 20,    wallColor: 'white',  },};const desiredHouse = {  bath: true,  kitchen: {    amenities: ['oven', 'stove', 'washer'],    wallColor: expect.stringMatching(/white|yellow/),  },};test('the house has my desired features', () => {  expect(houseForSale).toMatchObject(desiredHouse);});
describe('toMatchObject applied to arrays', () => {  test('the number of elements must match exactly', () => {    expect([{foo: 'bar'}, {baz: 1}]).toMatchObject([{foo: 'bar'}, {baz: 1}]);  });  test('.toMatchObject is called for each elements, so extra object properties are okay', () => {    expect([{foo: 'bar'}, {baz: 1, extra: 'quux'}]).toMatchObject([      {foo: 'bar'},      {baz: 1},    ]);  });});

.toMatchSnapshot(propertyMatchers?, hint?)

This ensures that a value matches the most recent snapshot. This ensures that a value matches the most recent snapshot. Check out the Snapshot Testing guide for more information.

You can provide an optional propertyMatchers object argument, which has asymmetric matchers as values of a subset of expected properties, if the received value will be an object instance. It is like toMatchObject with flexible criteria for a subset of properties, followed by a snapshot test as exact criteria for the rest of the properties. It is like toMatchObject with flexible criteria for a subset of properties, followed by a snapshot test as exact criteria for the rest of the properties.

You can provide an optional hint string argument that is appended to the test name. You can provide an optional hint string argument that is appended to the test name. Although Jest always appends a number at the end of a snapshot name, short descriptive hints might be more useful than numbers to differentiate multiple snapshots in a single it or test block. Jest sorts snapshots by name in the corresponding .snap file. Jest sorts snapshots by name in the corresponding .snap file.

.toMatchInlineSnapshot(propertyMatchers?, inlineSnapshot)

Ensures that a value matches the most recent snapshot.

You can provide an optional propertyMatchers object argument, which has asymmetric matchers as values of a subset of expected properties, if the received value will be an object instance. It is like toMatchObject with flexible criteria for a subset of properties, followed by a snapshot test as exact criteria for the rest of the properties. It is like toMatchObject with flexible criteria for a subset of properties, followed by a snapshot test as exact criteria for the rest of the properties.

Jest adds the inlineSnapshot string argument to the matcher in the test file (instead of an external .snap file) the first time that the test runs.

Check out the section on Inline Snapshots for more info.

.toStrictEqual(value)

Use .toStrictEqual to test that objects have the same types as well as structure.

Differences from .toEqual:

  • Keys with undefined properties are checked. Keys with undefined properties are checked. e.g. {a: undefined, b: 2} does not match {b: 2} when using .toStrictEqual.

  • Array sparseness is checked. Array sparseness is checked. e.g. [, 1] does not match [undefined, 1] when using .toStrictEqual.

  • Object types are checked to be equal. Object types are checked to be equal. e.g. A class instance with fields a and b will not equal a literal object with fields a and b.

class LaCroix {  constructor(flavor) {    this.flavor = flavor;  }}describe('the La Croix cans on my desk', () => {  test('are not semantically the same', () => {    expect(new LaCroix('lemon')).toEqual({flavor: 'lemon'});    expect(new LaCroix('lemon')).not.toStrictEqual({flavor: 'lemon'});  });});

.toThrow(error?)

Also under the alias: .toThrowError(error?)

Use .toThrow to test that a function throws when it is called. Use .toThrow to test that a function throws when it is called. For example, if we want to test that drinkFlavor('octopus') throws, because octopus flavor is too disgusting to drink, we could write:

test('throws on octopus', () => {  expect(() => {    drinkFlavor('octopus');  }).toThrow();});

Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail.

You can provide an optional argument to test that a specific error is thrown:

  • regular expression: error message matches the pattern

  • string: error message includes the substring

  • error object: error message is equal to the message property of the object

  • error class: error object is instance of class

For example, let’s say that drinkFlavor is coded like this:

function drinkFlavor(flavor) {  if (flavor == 'octopus') {    throw new DisgustingFlavorError('yuck, octopus flavor');  }  // Do some other stuff}

We could test this error gets thrown in several ways:

test('throws on octopus', () => {  function drinkOctopus() {    drinkFlavor('octopus');  }  // Test that the error message says "yuck" somewhere: these are equivalent  expect(drinkOctopus).toThrowError(/yuck/);  expect(drinkOctopus).toThrowError('yuck');  // Test the exact error message  expect(drinkOctopus).toThrowError(/^yuck, octopus flavor$/);  expect(drinkOctopus).toThrowError(new Error('yuck, octopus flavor'));  // Test that we get a DisgustingFlavorError  expect(drinkOctopus).toThrowError(DisgustingFlavorError);});

.toThrowErrorMatchingSnapshot(hint?)

Use .toThrowErrorMatchingSnapshot to test that a function throws an error matching the most recent snapshot when it is called.

You can provide an optional hint string argument that is appended to the test name. You can provide an optional hint string argument that is appended to the test name. Although Jest always appends a number at the end of a snapshot name, short descriptive hints might be more useful than numbers to differentiate multiple snapshots in a single it or test block. Jest sorts snapshots by name in the corresponding .snap file. Jest sorts snapshots by name in the corresponding .snap file.

For example, let’s say you have a drinkFlavor function that throws whenever the flavor is 'octopus', and is coded like this:

function drinkFlavor(flavor) {  if (flavor == 'octopus') {    throw new DisgustingFlavorError('yuck, octopus flavor');  }  // Do some other stuff}

The test for this function will look this way:

test('throws on octopus', () => {  function drinkOctopus() {    drinkFlavor('octopus');  }  expect(drinkOctopus).toThrowErrorMatchingSnapshot();});

And it will generate the following snapshot:

exports[`drinking flavors throws on octopus 1`] = `"yuck, octopus flavor"`;

Check out React Tree Snapshot Testing for more information on snapshot testing.

.toThrowErrorMatchingInlineSnapshot(inlineSnapshot)

Use .toThrowErrorMatchingInlineSnapshot to test that a function throws an error matching the most recent snapshot when it is called.

Jest adds the inlineSnapshot string argument to the matcher in the test file (instead of an external .snap file) the first time that the test runs.

Check out the section on Inline Snapshots for more info.