[英]What Jest matcher should I use for functions?
Let's say that a function returns another function.假设一个 function 返回另一个 function。
function foo() {
return () => 1;
}
What matcher can I use after expect(foo())
to make sure that foo()
returns a function?在
expect(foo())
之后我可以使用什么匹配器来确保foo()
返回 function? Also, is there a way to also verify function signature?另外,有没有办法验证 function 签名?
typeof has it's fair share of quirks, but I think it seems sensible to use in this scenario. typeof 有相当多的怪癖,但我认为在这种情况下使用它似乎是明智的。
expect( typeof foo() ).toBe('function')
You can check how many arguments a function is defined with using .length
您可以使用
.length
检查 function 定义了多少个 arguments
expect( (a, b, c) => {} ).toHaveLength( 3 )
But there is no way to enforce the types of arguments in javascript.但是没有办法在 javascript 中强制执行 arguments 的类型。 All functions are able to accept any types.
所有函数都能够接受任何类型。 And you can always pass more arguments than "specified" to a function.
而且您总是可以将比“指定”更多的 arguments 传递给 function。 And that function might use
arguments
to read all those arguments, so even .length
might be of limited use for your use case.并且 function 可能会使用
arguments
来读取所有这些 arguments,因此即使.length
对您的用例的使用也可能有限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.