【发布时间】:2021-10-01 09:58:39
【问题描述】:
在我的模块中编写节点 CLI 我有一个 console.log 和 chalk,例如:
console.log(
chalk.green(`${delta}:`),
chalk.white(`\nAPI: ${apiPath}`),
)
当我运行 Jest 代码覆盖率 --coverage 时,我被提醒我没有测试所以我写道:
test(`Test console log`, async () => {
await mod(params)
await expect(console.log).toBe(`${delta}:\nAPI: ${apiPath}`)
})
但我收到以下错误:
Expected: "string"
Received: [Function log]
我尝试过的研究的第二次尝试:
test(`Test console log`, async () => {
await mod(params)
await expect(console.log).toHaveBeenCalledWith(`${delta}:\nAPI: ${apiPath}`)
})
但我收到以下错误:
Received has type: function
Received has value: [Function log]
研究:
- How do I test a jest console.log
- Console.log statements output nothing at all in Jest
- How to test console.log output using Jest or other Javascript Testing Framework?
使用 Jest 如何测试使用粉笔的 console.log?
【问题讨论】:
标签: node.js unit-testing jestjs chalk