NodeJS 如何jest test(Body as any).pipe(zlib.createGunzip())?

p4rjhz4m  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(90)

我一直在创建一个单元测试来测试我的导入函数实现,但是我不能从.pipe()行通过。以下是我的一些函数行,以获得更多上下文:

const { Body } = await s3.send(command);
  console.log('body');
  console.log(Body);
  const input = (Body as any).pipe(zlib.createGunzip());
  console.log('done body');
  const lines = readline.createInterface({
    input,
    crlfDelay: Infinity,
  });
  console.log('done lines');

字符串
下面是我的单元测试代码:

describe('Import function', function () {
  it('Import file', async () => {
    const stream = createReadStream('./tests/data/test.csv');
    const sdkStream = sdkStreamMixin(stream);
    s3Mock.on(GetObjectCommand).resolves({ Body: sdkStream });

    const event: any;
    let context: any;
    const result = await handler(event, context);
    console.log(result);
  });
});


正在打印console.log('body');console.log(Body);行,Body具有下一个值:

console.log
      ReadStream {
        fd: null,
        path: './tests/data/GLOBAL.csv',
        flags: 'r',
        mode: 438,
        start: undefined,
        end: Infinity,
        pos: undefined,
        bytesRead: 0,
        _readableState: ReadableState {
          objectMode: false,
          highWaterMark: 65536,
          ...


即使Body有一个值,当我运行测试时,我得到了下一个错误:

console.log
      TypeError: Cannot read properties of undefined (reading 'on')
          at ReadStream.Readable.pipe (node:internal/streams/readable:711:8)


知道发生什么事了吗
我使用以下代码实现了s3 read mockup:https://www.npmjs.com/package/aws-sdk-client-mock之后,我尝试向s3Mock对象添加另一个.on值,但没有成功。

luaexgnf

luaexgnf1#

最后,我不得不将这些代码行移到一个新文件中,这样我就可以模拟文件导入

相关问题