node.js - Jest - promisify ile sarılmış zlib fonksiyonunu mocklamak çalışmıyor
Bir dosyayı test etmeye çalışıyorum; bu dosya promisify ile sarılmış zlib kullanıyor, ancak test kodda zlib kullanan satıra ulaşınca jest zaman aşımı hatası alıyorum.
: 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:Modül dosyası:
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;
}
}
Test Dosyası
jest.mock('zlib');
import zlib from 'zlib';
let gunzipMock: jest.SpyInstance;
gunzipMock = jest.spyOn(zlib, 'gunzip');
gunzipMock.mockResolvedValue(JSON.stringify(problemZipContent));
Testi hata ayıkladığımda, await zlibGunzip(buff); çağrısına ulaştığını görüyorum ama sonra bir hata fırlatılıyor. Catch bloğuna da ulaşmıyor.
Bunu nasıl test edebileceğim konusunda lütfen bilgi verin. Teşekkür ederim
Cevap #1
Zlib.gunzip() yöntemini ve onun uygulamasını taklit edin, çünkü bu yöntemin ikinci argümanı bir Node.js hata-öncelikli geri arama (callback) fonksiyonudur, testinizde callback fonksiyonunu manuel olarak çağırmanız gerekir. Bu sayede util.promisify(zlib.gunzip) tarafından döndürülen promise çözümlenmiş (resolve) olacaktır.
Örn.
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);
});
});
test sonucu:
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
