【发布时间】:2021-08-01 04:00:01
【问题描述】:
我正在尝试测试一个使用 zlib 的文件,该文件包含在 promisify 中,但是当测试到达代码中使用 zlib 的行时,我得到一个开玩笑的超时错误。
: Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.Error:
模块文件:
import zlib from 'zlib';
import util from 'util';
const zlibGunzip = util.promisify(zlib.gunzip);
async function unzipObjectContent(objectBody): Promise<string> {
const buff = objectBody as Buffer;
try {
const data = await zlibGunzip(buff);
const utf8String = data.toString('utf8');
const parsedJson = JSON.parse(utf8String);
return JSON.stringify(parsedJson);
} catch (error) {
logger.error(`unzipObjectContent -> failed to unzip file ${error}`);
throw error;
}
}
测试文件
jest.mock('zlib');
import zlib from 'zlib';
let gunzipMock: jest.SpyInstance;
gunzipMock = jest.spyOn(zlib, 'gunzip');
gunzipMock.mockResolvedValue(JSON.stringify(problemZipContent));
当我调试测试时,我看到它到达了对await zlibGunzip(buff); 的调用,但随后抛出了一个错误。它也没有到达 catch 块。
请告知我该如何测试。 谢谢
【问题讨论】:
标签: node.js typescript jestjs mocking es6-promise