javascript - 为什么模拟 axios get 方法返回未定义?
问题描述
我编写了一个相当简单的异步方法,通过 HTTP 检索结果:
import axios from "axios";
const BASE_URI = "http://api.tvmaze.com";
export const getSearchShows = async (search: string) => {
const uri = `${BASE_URI}/search/shows?q=${encodeURIComponent(search)}`;
const response = await axios.get(uri);
return response.data;
};
我想对它进行单元测试。所以我写了下面的 Jest 测试,它打算模拟 axios 并返回假结果,然后我可以断言:
import axios from "axios";
import fakeSearchShowsResponse from "../data/search-shows--q=test.json";
import { getSearchShows } from "./TvShows.http";
jest.mock("axios");
describe("TvShows.http", () => {
describe("getSearchShows", () => {
it("retrieves shows over http and correctly deserializes them", async () => {
const mockAxiosGet = jest.spyOn(axios, "get");
mockAxiosGet.mockImplementation(async () => fakeSearchShowsResponse);
const shows = await getSearchShows("test");
console.log(mockAxiosGet.mock.calls);
expect(shows[0].id).toEqual(139);
});
});
});
我预计,由于调用jest.mock("axios")
,axios get 方法将被替换为模拟的 Jest 方法。
此外,我预计由于调用mockAxiosGet.mockImplementation
并传递了一个函数,对 axios get 方法的调用实际上会调用我的模拟函数,从而允许我用测试数据替换真实数据。
实际发生的是对 axios get 的调用返回undefined
,导致我的测试断言失败。
然而,奇怪的是,Jest 间谍仍然记录该方法被调用——console.log 输出一个调用。
undefined
那么,当我明确提供了一个返回值的模拟实现时,为什么这个所谓的模拟方法会返回呢?
还是我误解了 mockImplementation 的使用方式?
解决方案
因此,经过一些实验后,似乎该jest.mock("axios")
呼叫正在干扰jest.spyOn(axios, "get");
呼叫。
删除 jest.mock 调用后,它现在从 jest.spyOn 调用返回我的模拟值。
我认为这可能是因为 jest.mock 调用被提升了,而 jest.spyOn 调用没有。因此,被测模块脱离了悬挂的模拟,而不是未悬挂的模拟。
推荐阅读
- ios - Chromecast 视频不会自动播放 m3u8 文件
- apache-kafka - Kafka 流聚合复制
- django - 我正在尝试运行迁移,但是当我运行 migrate.py 时出现类型错误?
- javascript - 我的间隔在 clearInterval 之后不会立即停止
- python - pyopencl中基本字符串复制程序中的Unicode解码错误
- eclipse-plugin - SWT 浏览器不会在 Red Hat Linux 中显示 pdf 文件,而是打开一个保存提示。有没有办法处理这个?
- dart - 兼容 Flutter 和 javascript 的 AES 加解密
- command-line - 如何从命令行在 Bitbucket 中创建拉取请求
- powershell - 导入 CSV,然后从一列中获取 AD 组信息
- php - 从下拉列表中选择后PHP文件未更新