如何用Jest模拟请求回调

时间:2020-09-11 16:12:14

标签: node.js unit-testing jestjs request-promise ts-jest

我正在开玩笑地模拟请求回调,但无法模拟

这是我的职责

await requestPromise.get(options, ((err, resp) => {
    if (err) {
        throw err;
    }
    if (resp && resp.headers['content-disposition']) {
        res.setHeader('Content-type', 'application/pdf');
        res.setHeader('content-disposition', resp.headers['content-disposition']);
        const pdfFile = Buffer.from(resp.body);
        res.send(pdfFile);
    }
})).catch((err: Error) => {
        throw err;
});

在我的测试中,我像通过了此测试一样在嘲笑,但是在覆盖率报告中,这些行未被测试覆盖

it('should download document', inject([PdfDocService, Res],
    async (service: PdfDocService, res: Res) => {

    const mockResp = {
        body: {},
        headers: { 'content-disposition': 'attachment; filename="Sathish_test.pdf"' },
    };
    jest.spyOn(requestPromise, 'get').mockResolvedValue(mockResp);
    await service.downloadPdf(documentId, res);
}));

0 个答案:

没有答案