node.js - Jest - mocking zlib-funksjon som er pakket inn i promisify fungerer ikke
Jeg prøver å teste en fil som bruker zlib som er pakket inn i promisify, men når testen når linjen i koden som bruker zlib, får jeg en jest timeout-feil.
: 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:Modulfil:
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;
}
}
Testfil
jest.mock('zlib');
import zlib from 'zlib';
let gunzipMock: jest.SpyInstance;
gunzipMock = jest.spyOn(zlib, 'gunzip');
gunzipMock.mockResolvedValue(JSON.stringify(problemZipContent));
Når jeg feilsøker testen, ser jeg at den når kallet til await zlibGunzip(buff);, men så kastes en feil. Den når heller ikke catch-blokken.
Vennligst gi råd om hvordan jeg kan teste dette. Takk
Svar #1
Lag en mock for zlib.gunzip()-metoden og dens implementering, siden det andre argumentet til denne metoden er en Node.js error-first callback, må du kalle callback-funksjonen manuelt i testen din. Slik at løftet som returneres fra util.promisify(zlib.gunzip) blir løst.
F.eks.
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);
});
});
testresultat:
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
