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 ...
随机推荐
- Flutter2.X学习之路--调试页面布局好用的办法
Flutter里有个很好用的东西,可以方便我们来进行页面组件的布局调试,话不多说,上代码 1.找到main.dart引入rendering.dart import 'package:flutter/r ...
- Java 进阶P-4.6+P-4.7
向上造型 造型cast 子类的对象可以赋值给父类的变量 注意! java中不存在对象对对象的赋值!! 父类的对象不能赋值给子类的变量 可以用造型: c=(Car) v; (只有当v这个变量实际管理的是 ...
- .Net6 微服务之Polly入门看这篇就够了
前言 O(∩_∩)O 大家好!书接上文,本文将会继续建立在 .Net6 使用 Ocelot + Consul 看这篇就够了 项目的基础上进行Polly的介绍,然后这篇文章只是个人学习与分享,不喜勿喷, ...
- 提供给用户使用的表格样式自定义工具,适用于elementUI表格
介绍 给用户提供了可以自定义修改elementUI表格的能力,通过混入(mixins)使用,必须先安装element-ui. 通过npm安装: npm i el-table-customizer 使用 ...
- @EnableDiscoveryClient和@EnableEurekaClient springboot3.x
@EnableDiscoveryClient和@EnableEurekaClient 将一个微服务注册到Eureka Server(或其他服务发现组件,例如Zookeeper.Consul等)的步骤 ...
- 【Vue】style和class 列表渲染 使用v-for进行循环 监控失效 双向数据绑定 过滤案例 事件修饰符
目录 昨日回顾 style和class class属性的三种设置方法 style属性的三种设置方法 条件渲染 列表渲染 使用v-for进行循环 循环数字 循环字符串 循环对象 循环数组 标签key值加 ...
- Idea的jdbc中的查询与增删该
在上一篇的折磨中 终于写好了代码 来总结一下其中的奥妙 (相同部分:)1.有mysql并且与主机建立连接 2.有jar包并且复制到自己创建的libs文件下,右键add 3.需要写main方法 4.需要 ...
- Java前后端请求Content-Type与接受方式
1.Get Get方法没有请求体,所以加不加Content-Type没有意义. 参数通过拼接到Url来加入 url?key=value&key2=value2 SpringMVC后台如何获取参 ...
- 郁金香 对MFC 编辑框的查看 与更改
非常简单,本来想写一个带窗口的DLL注入工具 但是进程句柄可以拿到,但是好像开辟不了空间 注入DLL进不去 不知道怎么回事 这个问题,日后解决
- 三天吃透Spring面试八股文(最新整理)
本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...