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:

最新文章

  1. ObjC运行时部分概念解析(二)
  2. 相克军_Oracle体系_随堂笔记007-PGA
  3. method
  4. Redis 资源
  5. Generic method return type
  6. POJ 3668 Game of Lines (暴力,判重)
  7. 单片微机原理P0:80C51结构原理
  8. Oracle listener lsnrctl
  9. SQL练习1关于插入删除,修改,单表查询
  10. PHP的虚拟域名的配置
  11. 【数论·错位排列】bzoj4517 排列计数
  12. win10下安装ubantu
  13. oracle BLOG图片和CLOG base64码的转换
  14. Linux下安装oracle的过程
  15. 深入浅出 kvm qemu libvirt
  16. JS模块化编程(一)
  17. English trip -- Phonics 5 元音字母 o
  18. [C/C++] multimap查找一个key对应的多个value
  19. MYSQL COST optimizer
  20. mongo 统计数据磁盘消耗

热门文章

  1. ArcGIS工具 - 计算折点数量
  2. 学习ASP.NET Core Blazor编程系列二十二——登录(1)
  3. webSocket前端+nodejs后端简单案例多人在线聊天
  4. Sundial (二)
  5. 【随笔记】NDK 编译开源库 SQLite3
  6. FLASH-CH32F103替换STM32F103 FLASH快速编程移植说明
  7. 我们从 CircleCI 安全事件获得的3个经验教训
  8. 从 Cloud-Native Relational DB 看数据库设计
  9. 12月16日内容总结——图书管理系统、聚合与分组查询、F与Q查询
  10. cnpm : 无法将“cnpm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。所在位置 行:1 字符: 1