【问题标题】:Mock Node.js stream pipeline using Jest使用 Jest 模拟 Node.js 流管道
【发布时间】:2021-03-31 19:20:56
【问题描述】:

我有一个使用 Node.js pipeline() 的方法,该方法具有回调的最后一个参数。

doSomething(readStream, destinationwritableStream): void {
   pipeline(readStream, destinationwritableStream, async () => {
      // some code here.
      // unable to cover this code using JEST
   })
}

任何建议,我如何涵盖此 async() 回调或如何模拟 pipeline() 方法。

提前致谢。

【问题讨论】:

  • 你能告诉我你如何导入pipeline函数吗?

标签: node.js jestjs stream mocking nodejs-stream


【解决方案1】:

你可以这样模拟它:

jest.mock('./../path/to/file/that/includes/pipeline');

const { pipeline } = require('./../path/to/file/that/includes/pipeline');


const pipelineMock = async () => {
  return true;
};

pipeline.mockImplementation(pipelineMock);

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2020-10-26
    • 2014-09-13
    • 2017-07-07
    • 2015-02-10
    • 1970-01-01
    • 2016-04-18
    • 2021-11-02
    相关资源
    最近更新 更多