ALa*_*osk 4 javascript unit-testing jestjs
如何正确测试(使用jest)结果是否是实际的 javascript 函数?
describe('', () => {
it('test', () => {
const theResult = somethingThatReturnsAFunction();
// how to check if theResult is a function
})
});
Run Code Online (Sandbox Code Playgroud)
我找到的唯一解决方案是使用 typeof 像这样:
expect(typeof handledException === 'function').toEqual(true);
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
Mar*_*hoh 21
Jest 提供了一种检查所提供值的类型的好方法。
您可以用来.toEqual(expect.any(<Constructor>))检查提供的值是否属于构造函数的类型:
describe('', () => {
it('test', () => {
const theResult = somethingThatReturnsAFunction()
expect(theResult).toEqual(expect.any(Function))
})
})
Run Code Online (Sandbox Code Playgroud)
构造函数的其他示例是:String& Number。
您可以使用toBe匹配器来检查typeof运算符的结果是否为function,请参见示例:
describe("", () => {
it("test", () => {
const somethingThatReturnsAFunction = () => () => {};
const theResult = somethingThatReturnsAFunction();
expect(typeof theResult).toBe("function");
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3381 次 |
| 最近记录: |