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的更多相关文章

  1. [Testing] Config jest to test Javascript Application -- Part 1

    Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...

  2. JavaScript单元测试工具-Jest

    标注: 首先这并不是一篇完整的关于Jest的教程,只是个人在接触jest学习的一点随手笔记,大部分内容都是对官方文档的一些翻译. ----------------------------------- ...

  3. Android application testing with the Android test framework

    目录(?)[-] Android automated testing 1 How to test Android applications Tip 2 Unit tests vs functional ...

  4. [转]awsome-java

    原文链接 Awesome Java A curated list of awesome Java frameworks, libraries and software. Contents Projec ...

  5. Awesome Java: Github上关于Java相关的工具

    Awesome Java 这是Github上关于Java相关的工具,框架等等资源集合. 原文参考: https://github.com/akullpp/awesome-java. @pdai 最全的 ...

  6. Android Weekly Notes Issue #233

    Android Weekly Issue #233 November 27th, 2016 Android Weekly Issue #233 本期内容包括: 用Mockito做RxJava的单元测试 ...

  7. Concurrency in C# Cookbook 笔记

    Pausing for a Period of TimeProblem:You need to (asynchronously) wait for a period of time. This can ...

  8. 前端测试框架Jest系列教程 -- Asynchronous(测试异步代码)

    写在前面: 在JavaScript代码中,异步运行是很常见的.当你有异步运行的代码时,Jest需要知道它测试的代码何时完成,然后才能继续进行另一个测试.Jest提供了几种方法来处理这个问题. 测试异步 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. Python读取大量Excel文件并跨文件批量计算平均值

      本文介绍基于Python语言,实现对多个不同Excel文件进行数据读取与平均值计算的方法.   首先,让我们来看一下具体需求:目前有一个文件夹,其中存放了大量Excel文件:文件名称是每一位同学的 ...

  2. RabbitMQ消息队列入门及解决常见问题

    RabbitMQ消息队列 同步通讯和异步通讯 微服务间通讯有同步和异步两种方式: 同步通讯:就像打电话,需要实时响应. 异步通讯:就像发邮件,不需要马上回复. 两种方式各有优劣,打电话可以立即得到响应 ...

  3. Docker不做虚拟化内核,对.NET有什么影响?

    引子 前两天刷抖音,看见了这样一个问题. 问题:容器化不做虚拟内核,会有什么弊端?Java很多方法会跟CPU的核数有关,这个时候调用系统函数,读到的是宿主机信息,而不是我们限制资源的大小. 思考:在我 ...

  4. 编程哲学之 C# 篇:003——为什么选择 C#

    国内开设C#课程的学校或培训机构是越来越少,使用C#作为开发的语言企业也是越来越少.企业要招C#的开发人员越来越难,会C#的要开发人员要找工作也是越来越难,然后我还是选择C#作为本系列的主要语言,我给 ...

  5. Pytest插件pytest-rerunfailures失败重跑

    Pytest插件pytest-rerunfailures失败重跑 安装 pip install pytest-rerunfailures doc https://github.com/pytest-d ...

  6. 在Flask中构建API接口的相关概念

    在Flask中构建API接口的相关概念 重定向行为 斜杠 以下两个路由的不同之处在于是否使用尾部的斜杠. 第一个路由的URL尾部有一个斜杠,看起来就像一个文件夹,访问一个没有斜杠结尾的URL时,Fla ...

  7. JavaScript 函数所能传递的最大参数

    取决于实现,取决于浏览器和操作系统标准没有规定(65535一般是有的) 来源:https://stackoverflow.com/questions/22747068/is-there-a-max-n ...

  8. Downie V4.6.4 for Mac 视频下载工具

    前言 Downie是Mac下一个简单的下载管理器,可以让您快速将不同的视频网站上的视频下载并保存到电脑磁盘里然后使用您的默认媒体播放器观看它们. ![在这里插入图片描述](https://p3-jue ...

  9. STM32F0_HAL初始化系列:串口DMA输出

    static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_ ...

  10. ft5426触摸屏I2C

    触摸的点数, 先写入地址0x38, 寄存器0x02, 再次读取0x38的数据,得到1个触摸点 读取全部坐标信息,需要读入30字节数据