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 ...
随机推荐
- vertica 7.0 使用kafka
config.propertiesclient.id设置为主机名或ip auto.offset.reset=smallest client.id=qas 设置环境变量 kafka_config=&qu ...
- 从0开始学Java 第一期 开发前的准备
Java 学习(一) - 开发前的准备 前言 由于一些项目上的需要,我得学习一下 Java 这门语言(主要是想写Android),本人并非0基础,至少在上个学期学习了一门必修的程序设计(C语言),所以 ...
- C語言成績分析系統
C語言成績分析系統,可以實現七個功能.(使用的編譯器是 code::blocks) 主要實現對於學生信息的輸入 顯示輸入學生的信息 根據期末成績來進行排名. 查找某個學生的信息 刪除某個學生的信息 修 ...
- qt虚拟键盘编译时报错缺乏qpa/qplatforminputcontext.h文件
ubuntu20.04 :sudo apt-get install qtbase5-private-dev
- Grafana 系列文章(六):Grafana Explore 中的日志
️URL: https://grafana.com/docs/grafana/latest/explore/logs-integration/#labels-and-detected-fields D ...
- VS针对Linux远程调试步骤
VS2019下对于远程Linux下C++代码的调试 VS2017后新增了对跨平台代码的编写,编译和调试的功能,2019后更是新增了多种插件,以下是针对C++版本的linux环境代码调试 准备工作 安装 ...
- zookeeper04---ZAB协议
转https://www.jianshu.com/p/2bceacd60b8a 1.什么是Zab协议 1.1Zab协议简介 1.2 Zab 协议的特性(怎么保持数据一致性) 2.Zab 协议实现的作用 ...
- 2211-12 Hello Flask!
本篇记录来自Flask 入门教程 第 2 章:Hello, Flask! 第 2 章:Hello, Flask! 追溯到最初,Flask 诞生于 Armin Ronacher 在 2010 年愚人节开 ...
- 如何通过Java 代码设置 Word 文档页边距
页边距是指页面的边线到文字的距离.通常可在页边距内部的可打印区域中插入文字和图形,也可以将某些项目放置在页边距区域中(如页眉.页脚和页码等).在我们用的Word文档中,都会设置页边距统一标准格式,页边 ...
- Windows家庭版安装本地组策略编辑器【gpedit.msc】
由于我们买的电脑一般默认安装的Windows家庭版本 家庭版本默认不带有本地组策略编辑器 当我们运行gpedit.msc的时候会提示 Winodws找不到文件'gpedit.msc'.请确定文件名是否 ...