GenericAssertions
GenericAssertions 类提供了断言方法,可用于对测试中的任何值进行断言。通过调用 expect() 创建 GenericAssertions 的新实例。
import { test, expect } from '@playwright/test';
test('assert a value', async ({ page }) => {
const value = 1;
expect(value).toBe(2);
});
方法
any
添加于:v1.9expect.any()
匹配从 构造函数 创建的任何对象实例或相应的原始类型。在 expect(value).toEqual() 中使用它来执行模式匹配。
用法
// Match instance of a class.
class Example {}
expect(new Example()).toEqual(expect.any(Example));
// Match any number.
expect({ prop: 1 }).toEqual({ prop: expect.any(Number) });
// Match any string.
expect('abc').toEqual(expect.any(String));
参数
anything
添加于:v1.9expect.anything()
匹配除 null
和 undefined
之外的所有内容。在 expect(value).toEqual() 中使用它来执行模式匹配。
用法
const value = { prop: 1 };
expect(value).toEqual({ prop: expect.anything() });
expect(value).not.toEqual({ otherProp: expect.anything() });
arrayContaining
添加于:v1.9expect.arrayContaining()
匹配包含预期数组中所有元素(顺序不限)的数组。请注意,接收到的数组可能是预期数组的超集,并包含一些额外的元素。
在 expect(value).toEqual() 中使用此方法来执行模式匹配。
用法
expect([1, 2, 3]).toEqual(expect.arrayContaining([3, 1]));
expect([1, 2, 3]).not.toEqual(expect.arrayContaining([1, 4]));
参数
closeTo
添加于:v1.9比较浮点数的近似相等性。在 expect(value).toEqual() 中使用此方法来执行模式匹配。如果只是比较两个数字,请优先使用 expect(value).toBeCloseTo()。
用法
expect({ prop: 0.1 + 0.2 }).not.toEqual({ prop: 0.3 });
expect({ prop: 0.1 + 0.2 }).toEqual({ prop: expect.closeTo(0.3, 5) });
参数
objectContaining
添加于:v1.9expect.objectContaining()
匹配包含并匹配预期对象中所有属性的对象。请注意,接收到的对象可能是预期对象的超集,并包含一些额外的属性。
在 expect(value).toEqual() 中使用此方法来执行模式匹配。对象属性可以是匹配器,以进一步放宽预期。参见示例。
用法
// Assert some of the properties.
expect({ foo: 1, bar: 2 }).toEqual(expect.objectContaining({ foo: 1 }));
// Matchers can be used on the properties as well.
expect({ foo: 1, bar: 2 }).toEqual(expect.objectContaining({ bar: expect.any(Number) }));
// Complex matching of sub-properties.
expect({
list: [1, 2, 3],
obj: { prop: 'Hello world!', another: 'some other value' },
extra: 'extra',
}).toEqual(expect.objectContaining({
list: expect.arrayContaining([2, 3]),
obj: expect.objectContaining({ prop: expect.stringContaining('Hello') }),
}));
参数
stringContaining
添加于:v1.9expect.stringContaining()
匹配包含预期子字符串的字符串。在 expect(value).toEqual() 中使用此方法来执行模式匹配。
用法
expect('Hello world!').toEqual(expect.stringContaining('Hello'));
参数
stringMatching
添加于:v1.9expect.stringMatching()
匹配与预期模式匹配的接收字符串。在 expect(value).toEqual() 中使用此方法来执行模式匹配。
用法
expect('123ms').toEqual(expect.stringMatching(/\d+m?s/));
// Inside another matcher.
expect({
status: 'passed',
time: '123ms',
}).toEqual({
status: expect.stringMatching(/passed|failed/),
time: expect.stringMatching(/\d+m?s/),
});
参数
toBe
添加于:v1.9通过调用 Object.is
将值与 expected 进行比较。此方法通过引用而不是内容来比较对象,类似于严格相等运算符 ===
。
用法
const value = { prop: 1 };
expect(value).toBe(value);
expect(value).not.toBe({});
expect(value.prop).toBe(1);
参数
toBeCloseTo
添加于:v1.9比较浮点数的近似相等性。在比较浮点数时,使用此方法而不是 expect(value).toBe()。
用法
expect(0.1 + 0.2).not.toBe(0.3);
expect(0.1 + 0.2).toBeCloseTo(0.3, 5);
参数
toBeDefined
添加于:v1.9确保值不是 undefined
。
用法
const value = null;
expect(value).toBeDefined();
toBeFalsy
添加于:v1.9确保值在布尔上下文中为 false,即 false
、0
、''
、null
、undefined
或 NaN
之一。当你不关心具体值时使用此方法。
用法
const value = null;
expect(value).toBeFalsy();
toBeGreaterThan
添加于:v1.9确保数字或大整数值 value > expected
。
用法
const value = 42;
expect(value).toBeGreaterThan(1);
参数
toBeGreaterThanOrEqual
添加于:v1.9确保数字或大整数值 value >= expected
。
用法
const value = 42;
expect(value).toBeGreaterThanOrEqual(42);
参数
toBeInstanceOf
添加于:v1.9确保值是类的实例。使用 instanceof
运算符。
用法
expect(page).toBeInstanceOf(Page);
class Example {}
expect(new Example()).toBeInstanceOf(Example);
参数
toBeLessThan
添加于:v1.9确保数字或大整数值 value < expected
。
用法
const value = 42;
expect(value).toBeLessThan(100);
参数
toBeLessThanOrEqual
添加于:v1.9确保数字或大整数值 value <= expected
。
用法
const value = 42;
expect(value).toBeLessThanOrEqual(42);
参数
toBeNaN
添加于:v1.9确保值是 NaN
。
用法
const value = NaN;
expect(value).toBeNaN();
toBeNull
添加于:v1.9确保值是 null
。
用法
const value = null;
expect(value).toBeNull();
toBeTruthy
添加于:v1.9确保值在布尔上下文中为 true,**除** false
、0
、''
、null
、undefined
或 NaN
之外的任何值。当你不关心具体值时使用此方法。
用法
const value = { example: 'value' };
expect(value).toBeTruthy();
toBeUndefined
添加于:v1.9确保值是 undefined
。
用法
const value = undefined;
expect(value).toBeUndefined();
toContain(expected)
添加于:v1.9确保字符串值包含预期的子字符串。比较是区分大小写的。
用法
const value = 'Hello, World';
expect(value).toContain('World');
expect(value).toContain(',');
参数
toContain(expected)
添加于:v1.9确保值是一个 Array
或 Set
并包含预期项。
用法
const value = [1, 2, 3];
expect(value).toContain(2);
expect(new Set(value)).toContain(2);
参数
toContainEqual
添加于:v1.9确保值是一个 Array
或 Set
并包含与预期项相等的项。
对于对象,此方法递归检查所有字段的相等性,而不是像 expect(value).toContain() 那样通过引用比较对象。
对于原始值,此方法等同于 expect(value).toContain()。
用法
const value = [
{ example: 1 },
{ another: 2 },
{ more: 3 },
];
expect(value).toContainEqual({ another: 2 });
expect(new Set(value)).toContainEqual({ another: 2 });
参数
toEqual
添加于:v1.9比较值的内容与 expected 的内容,执行“深度相等”检查。
对于对象,此方法递归检查所有字段的相等性,而不是像 expect(value).toBe() 那样通过引用比较对象。
对于原始值,此方法等同于 expect(value).toBe()。
用法
const value = { prop: 1 };
expect(value).toEqual({ prop: 1 });
非严格相等
expect(value).toEqual() 执行深度相等检查,比较接收值和预期值的内容。要确保两个对象引用相同的实例,请改用 expect(value).toBe()。
expect(value).toEqual() 忽略 undefined
属性和数组项,并且不强制要求对象类型相等。为了更严格的匹配,请使用 expect(value).toStrictEqual()。
模式匹配
expect(value).toEqual() 还可以借助以下匹配器对对象、数组和原始类型执行模式匹配
- expect(value).any()
- expect(value).anything()
- expect(value).arrayContaining()
- expect(value).closeTo()
- expect(value).objectContaining()
- expect(value).stringContaining()
- expect(value).stringMatching()
以下是一个断言复杂对象中某些值的示例
expect({
list: [1, 2, 3],
obj: { prop: 'Hello world!', another: 'some other value' },
extra: 'extra',
}).toEqual(expect.objectContaining({
list: expect.arrayContaining([2, 3]),
obj: expect.objectContaining({ prop: expect.stringContaining('Hello') }),
}));
参数
toHaveLength
添加于:v1.9确保值的 .length
属性等于 expected。适用于数组和字符串。
用法
expect('Hello, World').toHaveLength(12);
expect([1, 2, 3]).toHaveLength(3);
参数
toHaveProperty
添加于:v1.9确保对象上存在指定 keyPath
处的属性,并可选地检查属性是否等于 expected。相等性会递归检查,类似于 expect(value).toEqual()。
用法
const value = {
a: {
b: [42],
},
c: true,
};
expect(value).toHaveProperty('a.b');
expect(value).toHaveProperty('a.b', [42]);
expect(value).toHaveProperty('a.b[0]', 42);
expect(value).toHaveProperty('c');
expect(value).toHaveProperty('c', true);
参数
toMatch
添加于:v1.9确保字符串值匹配正则表达式。
用法
const value = 'Is 42 enough?';
expect(value).toMatch(/Is \d+ enough/);
参数
toMatchObject
添加于:v1.9比较值的内容与 expected 的内容,执行“深度相等”检查。与 expect(value).toEqual() 不同,允许值中存在额外的属性,因此你可以只检查对象属性的子集。
比较数组时,项的数量必须匹配,并且每个项都会递归检查。
用法
const value = {
a: 1,
b: 2,
c: true,
};
expect(value).toMatchObject({ a: 1, c: true });
expect(value).toMatchObject({ b: 2, c: true });
expect([{ a: 1, b: 2 }]).toMatchObject([{ a: 1 }]);
参数
toStrictEqual
添加于:v1.9比较值的内容与 expected 的内容,**以及**它们的类型。
与 expect(value).toEqual() 的区别
- 会检查具有 undefined 属性的键。例如,
{ a: undefined, b: 2 }
不匹配{ b: 2 }
。 - 会检查数组的稀疏性。例如,
[, 1]
不匹配[undefined, 1]
。 - 会检查对象类型是否相等。例如,一个具有字段
a
和b
的类实例将不等于一个具有字段a
和b
的字面量对象。
用法
const value = { prop: 1 };
expect(value).toStrictEqual({ prop: 1 });
参数
toThrow
添加于:v1.9调用函数并确保它抛出错误。
可选地将错误与 expected 进行比较。允许的预期值
- 正则表达式 - 错误消息应**匹配**该模式。
- 字符串 - 错误消息应**包含**该子字符串。
- 错误对象 - 错误消息应**等于**该对象的 message 属性。
- 错误类 - 错误对象应是该类的**实例**。
用法
expect(() => {
throw new Error('Something bad');
}).toThrow();
expect(() => {
throw new Error('Something bad');
}).toThrow(/something/);
expect(() => {
throw new Error('Something bad');
}).toThrow(Error);
参数
toThrowError
添加于:v1.9用法
expect(() => {
throw new Error('Something bad');
}).toThrowError();
参数
属性
not
添加于:v1.9使断言检查相反的条件。例如,以下代码通过
const value = 1;
expect(value).not.toBe(2);
用法
expect(value).not
类型