expect.not.objectContaining(object)
匹配任何未递归地匹配预期属性的接收对象。也就是说预期对象不是接收对象的子集。因此,它匹配所接收的对象,该对象包含不属于预期对象的属性。它与expect. objectcontains相反。
describe('not.objectContaining', () => { const expected = {foo: 'bar'}; it('matches if the actual object does not contain expected key: value pairs', () => { expect({bar: 'baz'}).toEqual(expect.not.objectContaining(expected)); }); }); |
expect.not.stringContaining(string)
匹配不包含确切期望字符串的接收字符串。它与expect.stringContaining.相反
describe('not.stringContaining', () => { const expected = 'Hello world!'; it('matches if the actual string does not contain the expected substring', () => { expect('How are you?').toEqual(expect.not.stringContaining(expected)); }); }); |
expect.not.stringMatching(string | regexp)
匹配不匹配预期regexp的接收字符串。它与expect.stringMatching.相反
describe('not.stringMatching', () => { const expected = /Hello world!/; it('matches if the actual string does not match the expected regex', () => { expect('How are you?').toEqual(expect.not.stringMatching(expected)); }); }); |
expect.objectContaining(object)
匹配递归地匹配预期属性的任何接收对象。也就是说,预期对象是接收对象的子集。因此,它匹配所接收的对象,该对象包含不属于预期对象的属性。
与期望对象中的文字属性值不同,您可以使用matchers、expect.anything()等等。
假设我们希望使用事件对象调用onPress函数,我们需要验证的是事件是否有event.x属性和y属性。我们可以这样做:
test('onPress gets called with the right thing', () => { const onPress = jest.fn(); simulatePresses(onPress); expect(onPress).toBeCalledWith( expect.objectContaining({ x: expect.any(Number), y: expect.any(Number), }), ); }); |
expect.stringMatching(string | regexp)
匹配与预期regexp匹配的接收字符串。你可以用它代替文字的值:
1. 在toEqual或toBeCalledWith
2. 匹配arraycontains中的元素
3. 匹配objectContaining 或者toMatchObject的属性
这个示例还展示了如何使用expect嵌套多个不对称的匹配器。在expect.arrayContaining stringMatching。
describe('stringMatching in arrayContaining', () => { const expected = [ expect.stringMatching(/^Alic/), expect.stringMatching(/^[BR]ob/), ]; it('matches even if received contains additional elements', () => { expect(['Alicia', 'Roberto', 'Evelina']).toEqual( expect.arrayContaining(expected), ); }); it('does not match if received does not contain expected elements', () => { expect(['Roberto', 'Evelina']).not.toEqual( expect.arrayContaining(expected), ); }); }); |
.toBe(value)
toBe只是检查一个值是否符合您的期望。它使用对象。是要检查完全相等。例如此代码将验证can对象的一些属性:
const can = { name: 'pamplemousse', ounces: 12, }; describe('the can', () => { test('has 12 ounces', () => { expect(can.ounces).toBe(12); }); test('has a sophisticated name', () => { expect(can.name).toBe('pamplemousse'); }); }); |
.toEqual(value)
如果要检查两个对象是否具有相同的值,请使用. toequal。此matcher递归地检查所有字段的相等性,而不是检查对象标识——这也称为“深度相等”。例如,toEqual和toBe在这个测试套件中表现不同,所以所有的测试都通过:
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); }); }); |
.toMatchObject(object)
使用. tomatchobject检查一个JavaScript对象是否匹配一个对象的属性子集。它将把接收到的对象与预期对象中没有的属性匹配起来。
您还可以传递一个对象数组,在这种情况下,只有当接收到的数组中的每个对象(在上面描述的番茄对象意义中)与预期数组中的相应对象相匹配时,该方法才会返回true。如果想要检查两个数组在它们的元素数量上是否匹配,而不是arrayinclude,这是非常有用的,因为它允许在接收的数组中添加额外的元素。
可以将属性匹配到值或匹配项。
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 arrays', () => { test('the number of elements must match exactly', () => { expect([{foo: 'bar'}, {baz: 1}]).toMatchObject([{foo: 'bar'}, {baz: 1}]); }); // .arrayContaining "matches a received array which contains elements that // are *not* in the expected array" test('.toMatchObject does not allow extra elements', () => { expect([{foo: 'bar'}, {baz: 1}]).toMatchObject([{foo: 'bar'}]); }); 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}, ]); }); }); |
.toHaveProperty(keyPath ,value)
使用. tohaveproperty检查在提供的引用keyPath中是否存在对象的属性。要检查对象中深度嵌套的属性,可以使用点表示法或包含深度引用的keyPath的数组。
可选地,你可以提供一个值来检查它是否等于目标对象的keyPath中的值。此matcher使用“深度相等”(如toEqual()))并递归地检查所有字段的相等性。
下面的示例包含一个带有嵌套属性的houseForSale对象。我们使用tohave属性来检查对象中各种属性的存在性和值。
// Object containing house features to be tested const houseForSale = { bath: true, bedrooms: 4, kitchen: { amenities: ['oven', 'stove', 'washer'], area: 20, wallColor: 'white', 'nice.oven': true, }, }; test('this house has my desired features', () => { // Simple Referencing expect(houseForSale).toHaveProperty('bath'); expect(houseForSale).toHaveProperty('bedrooms', 4); expect(houseForSale).not.toHaveProperty('pool'); // Deep referencing using dot notation expect(houseForSale).toHaveProperty('kitchen.area', 20); expect(houseForSale).toHaveProperty('kitchen.amenities', [ 'oven', 'stove', 'washer', ]); expect(houseForSale).not.toHaveProperty('kitchen.open'); // Deep referencing using an array containing the keyPath expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); expect(houseForSale).toHaveProperty( ['kitchen', 'amenities'], ['oven', 'stove', 'washer'], ); expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven']); expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); }); |