在Jest中进行XHR测试 [英] XHR testing in Jest

查看:318
本文介绍了在Jest中进行XHR测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试AJAX方法(vanilla XHR),我找不到使用 Jest 框架的方法。我找到了Jasmine的 mock-ajax.js ,问题是我找不到安装它的方法。

I want to test AJAX methods (vanilla XHR) and I can't find the way to do it with Jest framework. I found mock-ajax.js for Jasmine, the problem is i can't find the way to install it.

是否有更好的方法在 Jest 中单元测试Ajax函数?

Is there maybe a better way to Unit Test Ajax functions in Jest?

推荐答案

jest api已经改变了一点。这就是我使用的。它没有做任何事情,但它足以渲染我的组件。

The jest api has changed a bit. This is what I use. It doesn't do anything but it's enough to render my components.

const xhrMockClass = () => ({
  open            : jest.fn(),
  send            : jest.fn(),
  setRequestHeader: jest.fn()
})

window.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass)

并在测试文件中:

import'../../__ mock __ / xhr-mock.js'

这篇关于在Jest中进行XHR测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆