【发布时间】:2019-06-26 17:13:11
【问题描述】:
我如何使用 jest 测试下面的 sn-p。我正在尝试测试winston自定义格式printf
// sample.js
import {aa:{b}} = require("thirparty-package")
const a = () => {
return b((log) => {
return `log message will be ${log.message}`
})
}
module.exports = {
a
}
// sample.test.js
const customFunctions = require('./sample')
test('should check b function is called and returns a string', () => {
expect(customFunctions.a).toHaveBeenCalled() // throwing error
//jest.fn() value must be a mock function or spy.
})
【问题讨论】:
-
目前还不清楚发生了什么。为什么要调用
a?你没有打电话。 -
那我怎么知道 b 会被调用。检查原始的 sn-p 它将为您提供更好的视图 github.com/winstonjs/winston#formats。在这里,如果我们需要一个 customFormat,我们可以给它,但 customFormat 不是一个函数,它只是一个变量。由于我无法对其进行测试,因此我将 customFormat 移出并将其作为函数尝试对其进行测试。但它不起作用
标签: javascript node.js jestjs