Jest - Testing Asynchronous
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:

Jest - Testing Asynchronous的更多相关文章
- [Testing] Config jest to test Javascript Application -- Part 1
Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...
- JavaScript单元测试工具-Jest
标注: 首先这并不是一篇完整的关于Jest的教程,只是个人在接触jest学习的一点随手笔记,大部分内容都是对官方文档的一些翻译. ----------------------------------- ...
- Android application testing with the Android test framework
目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...
- [转]awsome-java
原文链接 Awesome Java A curated list of awesome Java frameworks, libraries and software. Contents Projec ...
- Awesome Java: Github上关于Java相关的工具
Awesome Java 这是Github上关于Java相关的工具,框架等等资源集合. 原文参考: https://github.com/akullpp/awesome-java. @pdai 最全的 ...
- Android Weekly Notes Issue #233
Android Weekly Issue #233 November 27th, 2016 Android Weekly Issue #233 本期内容包括: 用Mockito做RxJava的单元测试 ...
- Concurrency in C# Cookbook 笔记
Pausing for a Period of TimeProblem:You need to (asynchronously) wait for a period of time. This can ...
- 前端测试框架Jest系列教程 -- Asynchronous(测试异步代码)
写在前面: 在JavaScript代码中,异步运行是很常见的.当你有异步运行的代码时,Jest需要知道它测试的代码何时完成,然后才能继续进行另一个测试.Jest提供了几种方法来处理这个问题. 测试异步 ...
- [Jest] Set up Testing Globals in an Application with Jest
For some React component testing, we have common setup in each test file: import { render } from 're ...
- [Testing] Config jest to test Javascript Application -- Part 3
Run Jest Watch Mode by default locally with is-ci-cli In CI, we don’t want to start the tests in wat ...
随机推荐
- (18)go-micro微服务ELK介绍
目录 一 什么是ELK 二 Beats的六种工具 三 ELK系统的特点 四 ELK+beats系统架构 五 ELK优点 六 最后 一 什么是ELK ELK是三个[开源软件]的缩写,分别表示:Elast ...
- DVWA靶场实战(九)——Weak Session IDS
DVWA靶场实战(九) 九.Weak Session IDS: 1.漏洞原理: Weak Session IDS也叫做弱会话,当用户登录后,在服务器就会创造一个会话(session),叫做会话控制,接 ...
- Spring Boot + WebSocket 实时监控异常
本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...
- Python内置对象(一)
Python内置对象(一) 分多次讲解 这部分相对比较简单,偶尔一些特殊的做法会强调下(前面加★) 总览 builtins = [_ for _ in dir(__builtins__) if not ...
- ASP.NET6 + Mongo + OData
准备工作 Docker环境 Mongo数据库 配置Mongo数据库 ASP.NET6 集成Mongo 安装MongoDB.Driver { "Logging": { "L ...
- ICSharpCode.SharpZipLib.Zip 解析时报错System.NotSupportedException: No data is available for encoding 936
分析原因 利用ICSharpCode.SharpZipLib.Zip进行APK解析时,因为APK内编译的名称为中文,查询微软开发文档936为gb2312中文编码 微软开发文档地址https://doc ...
- JZOJ 3566. 【GDKOI2014】阶乘
题目 求十进制 \(n!\) 在 \(m\) 进制下末尾 \(0\) 的个数 分析 签到题 只要看 \(n!\) 有多少个 \(m\) 的倍数就好了 考虑分解 \(m\) 的质因子 然后根号计算每个因 ...
- Cesium之Quick Start
1. 引言 Cesium是一款三维地球和地图可视化开源JavaScript库,使用WebGL来进行硬件加速图形,使用时不需要任何插件支持,基于Apache2.0许可的开源程序,可以免费用于商业和非商业 ...
- js控制关闭layui的switch开关
<input class="switch" type="checkbox" lay-skin="switch" lay-filter= ...
- git 日常基本使用
// 将远程仓库下的所有分支拉取到本地 git fetch origin // 将dev分支合并到当前所在的分支 git merge dev // 基于当前分支克隆出新的本地分支 git checko ...