【发布时间】:2018-08-25 18:52:53
【问题描述】:
我正在尝试使用 Jest 监视已传递给异步函数的参数。
我的非常简单的模块...
// data_loader.js
import axios from "axios";
export const data_loader = ( options ) => {
return axios( generateUrl( options ) )
.then( response => response.data );
};
还有我的测试...
// data_loader.test.js
import * as data_loader from "./data_loader";
// this spies on the "load" function and passes
it("should be called once", () => {
const spy = data_loader.load = jest.fn();
data_loader.load(options);
expect(spy).toHaveBeenCalledTimes(1);
});
// here's where i'm trying to spy on axios to see if it is called with appropriate arguments
it("axios should be called with the supplied options", () => {
// have tried all syntax under the sun
});
如果我理解正确,我认为我需要模拟 axios,因为没有理由需要真正调用它来检查它收到的参数。
我已经尝试了jest.mock("axios")、jest.spyOn("axios") 和几个 Axios 的模拟库,但我无法理解语法。
【问题讨论】:
标签: javascript unit-testing axios jestjs