Jest - Testing Asynchronous
2024-10-21 13:42:46
When we test asynchronous, we use Axios to get the response.
install the Axios
npm i axios
Cause we already config the babel for presets, so we can directly use 'import'
fetchData.js
import axios from "axios";
export const fetchData = (fn) => {
axios.get('https://ning-xin.com/mock/jestTest.json').then((response) => {
fn(response.data)
})
}
export const fetchData2 = ()=>{
return axios.get('https://ning-xin.com/mock/jestTest.json')
}
fetchData.test.js (I named fetchData.test,.js. There is a comma, therefore, there is an error like 'No tests found')
import {fetchData, fetchData2} from "./asyncMethod";
test('fetch data test', (done) => {
fetchData((data) => {
try {
expect(data).toEqual({
"code": "200",
"data": "success"
}
)
done();
} catch (error) {
done(error);
}
})
})
test('fetch data test', (done) => {
fetchData((data) => {
try {
expect(data.code).toEqual("200")
done();
} catch (error) {
done(error);
}
})
})
test('Fetch promise data test', async (done) =>{
const response = await fetchData2();
const data = response.data;
console.log(data, 'response.data')
expect(data).toEqual({
"code": "200",
"data": "success"
}
)
})
Result:
最新文章
- ObjC运行时部分概念解析(二)
- 相克军_Oracle体系_随堂笔记007-PGA
- method
- Redis 资源
- Generic method return type
- POJ 3668 Game of Lines (暴力,判重)
- 单片微机原理P0:80C51结构原理
- Oracle listener lsnrctl
- SQL练习1关于插入删除,修改,单表查询
- PHP的虚拟域名的配置
- 【数论·错位排列】bzoj4517 排列计数
- win10下安装ubantu
- oracle BLOG图片和CLOG base64码的转换
- Linux下安装oracle的过程
- 深入浅出 kvm qemu libvirt
- JS模块化编程(一)
- English trip -- Phonics 5 元音字母 o
- [C/C++] multimap查找一个key对应的多个value
- MYSQL COST optimizer
- mongo 统计数据磁盘消耗
热门文章
- ArcGIS工具 - 计算折点数量
- 学习ASP.NET Core Blazor编程系列二十二——登录(1)
- webSocket前端+nodejs后端简单案例多人在线聊天
- Sundial (二)
- 【随笔记】NDK 编译开源库 SQLite3
- FLASH-CH32F103替换STM32F103 FLASH快速编程移植说明
- 我们从 CircleCI 安全事件获得的3个经验教训
- 从 Cloud-Native Relational DB 看数据库设计
- 12月16日内容总结——图书管理系统、聚合与分组查询、F与Q查询
- cnpm : 无法将“cnpm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。所在位置 行:1 字符: 1