【自动化测试】【Jest-Selenium】(03)—— Jest 异步测试
时间:2022-07-26
本文章向大家介绍【自动化测试】【Jest-Selenium】(03)—— Jest 异步测试,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1. 异步测试哪里特殊?
在JavaScript中执行异步代码是很常见的。当你有以异步方式运行的代码时,Jest 需要知道当前它测试的代码是否已完成,然后它可以转移到另一个测试。
经典错误:
-
默认情况下,Jest 测试一旦执行到末尾就会完成。问题在于一旦 fetchData 执行结束,此测试就在没有调用回调函数前结束。
function fetchData(callback) {
setTimeout(() => callback('peanut butter'), 5 * 1000);
}
// 经典错误
test('the data is peanut butter', () => {
function callback(data) {
expect(data).toBe('peanut butter');
}
fetchData(callback);
});
2. 异步测试基本模式
2.1. test('...', (done) => {...}) 模式
- Jest will wait if you provide an argument to the test function, usually called done.
- 默认测试超时时间:5s
- 可通过 testTimeout 选项配置
- 默认测试超时时间:5s
function fetchData(callback) {
setTimeout(() => callback('peanut butter'), 5 * 1000);
}
test('the data is peanut butter', (done) => {
function callback(data) {
expect(data).toBe('peanut butter');
done();
}
fetchData(callback);
});
2.2. Promise 模式
- If a promise is returned from test, Jest will wait for the promise to resolve before letting the test complete.
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => resolve('peanut butter'), 5 * 1000);
});
}
test('the data is peanut butter', () => {
return fetchData().then((data)=>{
expect(data).toBe('peanut butter');
});
});
2.3. Async|Await 模式
- Alternatively, you can use async and await in your tests.
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => resolve('peanut butter'), 5 * 1000);
});
}
test('the data is peanut butter', async () => {
const data = await fetchData();
expect(data).toBe('peanut butter');
});
3. expect.hasAssertions()、expect.assertions(number)
观察一个例子:
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => resolve('peanut butter'), 5 * 1000);
});
}
test('the data is peanut butter', async () => {
try {
await fetchData();
} catch (e) {
expect(e).toMatch('error');
}
});
这个例子中,显然,代码不会运行到 "expect(e).toMatch('error')",但测试用例还是通过了,这显然不是我们想要的。
-
expect.hasAssertions()
- expect.hasAssertions() verifies that at least one assertion is called during a test. This is often useful when testing asynchronous code, in order to make sure that assertions in a callback actually got called.
-
expect.assertions(number)
- expect.assertions(number) verifies that a certain number of assertions are called during a test. This is often useful when testing asynchronous code, in order to make sure that assertions in a callback actually got called.
修改一下例子:
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => resolve('peanut butter'), 5 * 1000);
});
}
test('the data is peanut butter', async () => {
expect.hasAssertions(); // 划重点
try {
await fetchData();
} catch (e) {
expect(e).toMatch('error');
}
});
再观察一个例子:
function doAsync(callback1, callback2) {
callback1(true);
}
test('the data is peanut butter', async () => {
expect.assertions(2); // 划重点
function callback1(data) {
expect(data).toBeTruthy();
}
function callback2(data) {
expect(data).toBeTruthy();
}
doAsync(callback1, callback2);
});
4. .resolves、.rejects
- Use resolves to unwrap the value of a fulfilled promise so any other matcher can be chained. If the promise is rejected the assertion fails.
- Use .rejects to unwrap the reason of a rejected promise so any other matcher can be chained. If the promise is fulfilled the assertion fails.
示例1:
function fetchData() {
return new Promise((resolve) => {
setTimeout(() => resolve('peanut butter'), 6 * 1000);
});
}
test('the data is peanut butter', () => {
return expect(fetchData()).resolves.toBe('peanut butter');
});
示例2:
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('peanut butter'));
}, 3 * 1000);
});
}
test('the data is peanut butter', async () => {
return expect(fetchData()).rejects.toThrow("peanut butter");
});
示例3:
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(1);
}, 3 * 1000);
}).then(()=>{
throw new Error('peanut butter');
});
}
test('the data is peanut butter', () => {
return expect(fetchData()).rejects.toThrow("peanut butter");
});
参考:
Jest Matchers: https://jestjs.io/docs/en/using-matchers Jest Expect API: https://jestjs.io/docs/zh-Hans/expect An Async Example: https://jestjs.io/docs/en/tutorial-async
- 协程解读
- nyoj-----幸运三角形
- HDUOJ --2523
- HDUOJ---1195Open the Lock
- 停止MySQL服务hang的问题简单分析(一)
- HDUOJ----2952Counting Sheep
- Golang语言社区--【基础知识】函数
- HDUOJ1060Leftmost Digit
- 轻量级交互数据json格式初探
- Golang语言社区--【基础知识】语言数组
- HDUOJ1086You can Solve a Geometry Problem too
- Golang语言社区--【基础知识】常量
- HDUOJ------1058 Humble Numbers
- MySQL偏移量的一点分析
- 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 数组属性和方法
- 换一种姿势挖掘任意用户密码重置漏洞
- 表哥,有没有XMLDecoder反序列化的案例?
- 不得不说,minigui真的很坑
- 树的遍历总结
- ggplot2|详解八大基本绘图要素
- LDheatmap|SNP连锁不平衡图(LD)可视化,倒三角图?
- Oracle 表分区笔记
- Java并发编程
- 让终端支持https,移植OpenSSL和libcurl到嵌入式linux,遇到的问题总结
- ComplexHeatmap|根据excel表绘制突变景观图(oncoplot)
- R-plotly|甘特图(Gantt chart)- 一不小心年中了,立个flag
- XXE漏洞那些事儿(JAVA)
- Hexo 建站过程
- Tidyverse|tidyr数据重塑之gather,spread(长数据宽数据转化)
- fastjson中的jndi注入