我正在尝试测试一个使用 zlib
的文件,该文件被包装在 promisify
中,但是当测试到达使用 zlib< 的代码行时
我收到一个 Jest 超时错误。
:超时 - 异步回调未在 jest.setTimeout.Timeout 指定的 30000 毫秒超时内调用 - 异步回调未在 jest.setTimeout.Error 指定的 30000 毫秒超时内调用:
模块文件:
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 block 。
请告知我如何测试它。 谢谢
最佳答案
模拟zlib.gunzip()
方法及其实现,因为这个方法的第二个参数是一个Node.js错误优先回调,你需要调用回调
在您的测试中手动运行。这样从 util.promisify(zlib.gunzip)
返回的 promise 将被解析。
例如
index.ts
:
import zlib from 'zlib';
import util from 'util';
const zlibGunzip = util.promisify(zlib.gunzip);
export 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) {
console.error(`unzipObjectContent -> failed to unzip file ${error}`);
throw error;
}
}
index.test.ts
:
import zlib from 'zlib';
import { unzipObjectContent } from './';
import { mocked } from 'ts-jest/utils';
jest.mock('zlib');
const mzlib = mocked(zlib);
describe('67475685', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', async () => {
const problemZipContent = Buffer.from(JSON.stringify({ name: 'teresa teng' }));
mzlib.gunzip.mockImplementationOnce((buffer, callback: any) => {
callback(null, problemZipContent);
});
const actual = await unzipObjectContent(problemZipContent);
expect(actual).toEqual('{"name":"teresa teng"}');
expect(mzlib.gunzip).toBeCalledTimes(1);
});
});
测试结果:
PASS examples/67475685/index.test.ts (7.156 s)
67475685
✓ should pass (4 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 83.33 | 100 | 100 | 83.33 |
index.ts | 83.33 | 100 | 100 | 83.33 | 16-17
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 7.656 s, estimated 8 s
关于node.js - Jest - 包装在 promisify 中的模拟 zlib 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67475685/