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 ...
随机推荐
- JavaScript 内存管理及垃圾回收
一.内存管理 JavaScript 是一种自动垃圾回收语言,这意味着 JavaScript 引擎会自动监测和清理无用的内存. JavaScript 中的内存管理主要由 JavaScript 引擎负责, ...
- jupyter的配置
step1.安装jupyter 使用pip或者conda等包管理工具安装jupyter(这部分倒是没有任何难度,一般也没有什么坑) conda install jupyter notebook或者pi ...
- Jemeter参数
以下是转载内容,仔细看过后,觉得用得最多的应该是csvread函数.用户自定义变量以及CSV DATA CONFIG控制器这几个,但是做练习之后,在结果树和聚合报告中怎么查看执行结果是个问题,没找到对 ...
- 【MRTK】HoloLens开发基础项目设置
前言 好记性不如烂笔头,之前做项目的时候很熟练很顺手就没有写笔记.因为排期问题项目中断几个月之后需要重新拾起来,结果发现自己现在忘记得差不多了,于是还是决定写点东西记录一下.即便是简单的项目设置,忘记 ...
- Linux之ssh远程连接
Linux之ssh远程连接 一.下载远程连接工具Xshell Xshell是一种远程连接工具,可用来远程连接虚拟机. Xshell免费版下载地址 输入名字和邮箱,可以在邮箱看到下载Xshell的链接. ...
- python学习第三周总结
文件操作 文件的读写模式 文件的操作模式 文件相关操作 文件内光标移动 文件内容修改 函数前戏 函数的语法结构 函数的定义和调用 函数的分类 函数的返回值 函数的参数 函数参数之位置参数 默认参数 可 ...
- Rust 的PIN的作用图
- 《关于我因为flink成为spark源码贡献者这件小事》
各位读者老爷请放下手上的板砖,我可真没有标题党,且容老弟慢慢道来. spark和flink本身相信我不用做过多的介绍,后端同学不管搞没搞过大数据,应该都多多少少听过. 如果没听过,简单说,spark和 ...
- TEB学习
官方资料:http://wiki.ros.org/teb_local_planner/Tutorials set up and test Optimization(重要) Inspect optimi ...
- JavaScript 日期和时间的格式化
一.日期和时间的格式化 1.原生方法 1.1.使用 toLocaleString 方法 Date 对象有一个 toLocaleString 方法,该方法可以根据本地时间和地区设置格式化日期时间.例如: ...