Jest - 包装在 promisify 中的模拟 zlib 函数不起作用

Jest - mocking zlib function which is wrapped in promisify does not work

我正在尝试测试一个使用 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 块。

请告知我如何测试它。 谢谢

模拟 zlib.gunzip() 方法及其实现,因为这个方法的第二个参数是一个 Node.js 错误优先回调,你需要在你的代码中手动调用 callback 函数测试。这样从 util.promisify(zlib.gunzip) 返回的承诺将得到解决。

例如

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