我想对这个方法进行单元测试(类似于How to use Jest to test file download?的启发):
export const download = (file, filename) => {
FileSaver.saveAs(file,filename);
};
这是测试
import { download } from "../../../utils/methods/utils";
import FileSaver from "file-saver";
import { item } from "../../__mocks__/responses/export"
describe("Test download", () => {
it("Can handle download", () => {
jest.mock("file-saver", () => ({
saveAs: jest.fn()
}));
download(item.file, "test.xls");
expect(FileSaver.saveAs).toHaveBeenCalledTimes(1);
});
});
但我得到
expect(received).toHaveBeenCalledTimes(expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function anonymous]
9 | }));
10 | download(item.file, "test.xls");
> 11 | expect(FileSaver.saveAs).toHaveBeenCalledTimes(1);
| ^
12 | });
13 | });
14 |
at Object.<anonymous> (src/__tests__/utils/methods/download.test.js:11:30)
1条答案
按热度按时间fhity93d1#
我用SpyOn a mocked jest module not spying properly解决了。
问题是一个小超时。