【自动化测试】【Jest-Selenium】(02)—— Jest 匹配器
1. 什么是匹配器?
使用不同匹配器可以测试输入输出的值是否符合预期。
例如:下面的 toBe、toBeGreaterThan 都是匹配器。
test('two plus two is four', () => {
expect(2 + 2).toBe(4);
expect(2 + 2).toBeGreaterThan(3);
});
2. 普通匹配
2.1. toBe(value)
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.
注意:Don't use .toBe with floating-point numbers.
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');
});
});
2.2. toBeNull()、toBeUndefined()、toBeDefined()
.toBeNull() is the same as .toBe(null)
test('null-defined', () => {
expect(null).toBeNull();
expect(undefined).not.toBeNull();
expect(undefined).toBeUndefined();
expect(null).not.toBeUndefined();
expect(null).toBeDefined();
});
2.3. toBeNaN()
Use .toBeNaN when checking a value is NaN.
test('passes when value is NaN', () => {
expect(NaN).toBeNaN();
expect(1).not.toBeNaN();
});
2.4. toBeTruthy()、toBeFalsy()
- Use .toBeTruthy when you don't care what a value is and you want to ensure a value is true in a boolean context.
- .Use .toBeFalsy when you don't care what a value is and you want to ensure a value is false in a boolean context.
test('toBeTruthy-toBeFalsy', () => {
expect(1).toBeTruthy();
expect("a").toBeTruthy();
expect({}).toBeTruthy();
expect([]).toBeTruthy();
expect(true).toBeTruthy();
expect("false").toBeTruthy();
expect("").toBeFalsy();
expect(0).toBeFalsy();
expect(false).toBeFalsy();
expect(NaN).toBeFalsy();
});
3. 字符串匹配
3.1. toMatch(regexpOrString)
Use .toMatch to check that a string matches a regular expression.
test('there is no I in team', () => {
expect('team').not.toMatch(/in team$/);
});
test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});
4. 数值匹配
- .toBeGreaterThan(number | bigint)
- received > expected
- .toBeGreaterThanOrEqual(number | bigint)
- received >= expected
- .toBeLessThan(number | bigint)
- received < expected
- .toBeLessThanOrEqual(number | bigint)
- received <= expected
- .toBeCloseTo(number, numDigits?)
- Use toBeCloseTo to compare floating point numbers for approximate equality.
test('number', () => {
expect(10).toBeGreaterThan(5);
expect(10).toBeGreaterThanOrEqual(10);
expect(10).toBeLessThan(20);
expect(10).toBeLessThanOrEqual(10);
expect(0.1 + 0.2).toBeCloseTo(0.3);
expect(0.1 + 0.2).not.toBe(0.3); // 特别注意
});
5. 数组匹配
5.1. 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.
test('', () => {
expect(["lime", "abc", "bcd"]).toContain('lime');
});
5.2. toHaveLength(number)
Use .toHaveLength to check that an object has a .length property and it is set to a certain numeric value.
test('', () => {
expect([1, 2, 3]).toHaveLength(3);
expect('abc').toHaveLength(3);
});
6. 对象匹配
6.1. toBeInstanceOf(Class)
Use .toBeInstanceOf(Class) to check that an object is an instance of a class.
test('', () => {
class A {}
expect(new A()).toBeInstanceOf(A);
expect(() => {}).toBeInstanceOf(Function);
expect(new A()).toBeInstanceOf(Object); // throws
});
6.2. 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.
test('', () => {
expect({
flavor: 'grapefruit',
ounces: 12,
}).toEqual({
flavor: 'grapefruit',
ounces: 12,
});
expect([1, 2, 3]).not.toBe([1, 2, 3]);
expect([1, 2, {
flavor: 'grapefruit',
ounces: 12,
}]).not.toBe([1, 2, {
flavor: 'grapefruit',
ounces: 12,
}]);
});
6.3. toHaveProperty(keyPath, value?)
Use .toHaveProperty to check if property at provided reference keyPath exists for an object.
- You can provide an optional
value
argument to compare the received property value (recursively for all properties of object instances, also known as deep equality, like thetoEqual
matcher).
// 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,
},
'ceiling.height': 2,
};
test('this house has my desired features', () => {
// Example 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']);
// Referencing keys with dot in the key itself
expect(houseForSale).toHaveProperty(['ceiling.height'], 2);
});
6.4. 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.
- 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.
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);
});
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},
]);
});
6.5. toStrictEqual(value)
Use .toStrictEqual to test that objects have the same types as well as structure.
- Keys with undefined properties are checked. e.g. {a: undefined, b: 2} does not match {b: 2} when using .toStrictEqual.
- Array sparseness is checked. e.g. [, 1] does not match [undefined, 1] when using .toStrictEqual.
- 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;
}
}
test('are not semantically the same', () => {
expect(new LaCroix('lemon')).toEqual({flavor: 'lemon'});
expect(new LaCroix('lemon')).not.toStrictEqual({flavor: 'lemon'});
});
7. 异常匹配
7.1. toThrow(error?)
Use .toThrow to test that a function throws when it is called.
function myfn(){
console.log(X);
}
test('throws on octopus', () => {
expect(() => {
myfn();
}).toThrow();
});
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- tensorflow 2.0模式下训练的模型转成 tf1.x 版本的pb模型实例
- Python如何实现后端自定义认证并实现多条件登陆
- safari下载文件自动加了html后缀问题
- yii2 上传图片的示例代码
- DataFrame.groupby()所见的各种用法详解
- keras训练曲线,混淆矩阵,CNN层输出可视化实例
- Pandas缺失值2种处理方式代码实例
- ThinkPHP5 的简单搭建和使用详解
- PyInstaller运行原理及常用操作详解
- php使用curl模拟浏览器表单上传文件或者图片的方法
- Python轻量级web框架bottle使用方法解析
- 详解在YII2框架中使用UEditor编辑器发布文章
- 基于python和flask实现http接口过程解析
- Python xpath表达式如何实现数据处理
- Python脚本破解压缩文件口令实例教程(zipfile)