Jest测试一个没有返回的函数

时间:2018-06-14 22:10:36

标签: javascript unit-testing testing jestjs

我正在使用Jest对函数进行单元测试,只是通过调用它将其参数传递给另一个函数并且没有返回。

我已经google了,尝试了一堆建议的模拟实现,但似乎无法让它工作。

导入的文件:

export const firstFunc = (dataObj) => {
  secondFunc('anything', dataObj)
}

export const secondFunc = (str, dataObj) => {
  // Does something with the dataObj but also doesn't return anything.
}

基本上我要测试的是firstFunc调用secondFunc,但无法让它工作。

提前谢谢你。另外,请接受我在这种情况下可以运行的任何其他测试的建议。

1 个答案:

答案 0 :(得分:1)

据我了解你的问题,你可以有两个解决方案。

首先,在调用函数后添加返回值,并将其导出到测试中。

其次,你可以使用sinon js和jest。

Spying on an imported function that calls another function in Jest