如何正确测试(使用jest)结果是否是实际的javascript函数?
describe('', () => {
it('test', () => {
const theResult = somethingThatReturnsAFunction();
// how to check if theResult is a function
})
});
我找到的唯一解决方案是使用类似这样的类型:
expect(typeof handledException === 'function').toEqual(true);
这是正确的方法吗?
答案 0 :(得分:1)
您可以使用toBe
匹配器检查typeof
运算符的结果是否为function
,请参阅示例:
describe("", () => {
it("test", () => {
const somethingThatReturnsAFunction = () => () => {};
const theResult = somethingThatReturnsAFunction();
expect(typeof theResult).toBe("function");
});
});