jest单元测试之更多
一、jest.fn
jest中的函数,具有跟踪性,可以测试是否被调用?调用几次?入参是否是期望值?
it("should jest fn", () => {
const jestFn = jest.fn((val) => {
return val;
});
const pramas = 1;
const res = jestFn(pramas);
expect(res).toBe(1);
expect(jestFn).toHaveBeenCalled();
expect(jestFn).toHaveBeenCalledTimes(1);
expect(jestFn).toHaveBeenCalledWith(pramas);
});
二、jest.mock
jest中的mock函数,当不会真的去发起函数调用,会使用定义的函数进行mock返回
it("should jest.mock", () => {
jest.mock("../src/06mock", () => {
return 5;
});
const mockFn = require("../src/06mock");
expect(mockFn).toBe(5);
});
三、jest.spyOn
jest.spyOn()
方法同样创建一个mock函数,但是该mock函数不仅能够捕获函数的调用情况,还可以正常的执行被spy的函数。实际上,jest.spyOn()
是jest.fn()
的语法糖,它创建了一个和被spy的函数具有相同内部代码的mock函数。
it("should jest.spyOn", () => {
const obj = require("../src/07spyOn");
const spy = jest.spyOn(obj.default, "get");
expect(spy()).toBe("牛");
expect(spy).toHaveBeenCalled();
});
it("should jest.spyOn", () => {
const obj = require("../src/07spyOn");
const spy = jest.spyOn(obj.default, "get").mockReturnValue("好牛");
expect(spy()).toBe("好牛");
expect(spy).toHaveBeenCalled();
});
四、更多
-
模拟延时
-
模拟class
-
模拟webpack,puppeteer等
…
未完待续...
代码参考github地址