user311413
user311413

Reputation: 177

What Jest matcher should I use for functions?

Let's say that a function returns another function.

function foo() {
  return () => 1;
}

What matcher can I use after expect(foo()) to make sure that foo() returns a function? Also, is there a way to also verify function signature?

Upvotes: 1

Views: 128

Answers (1)

ippi
ippi

Reputation: 10167

typeof has it's fair share of quirks, but I think it seems sensible to use in this scenario.

expect( typeof foo() ).toBe('function')

You can check how many arguments a function is defined with using .length

expect( (a, b, c) => {} ).toHaveLength( 3 )

But there is no way to enforce the types of arguments in javascript. All functions are able to accept any types. And you can always pass more arguments than "specified" to a function. And that function might use arguments to read all those arguments, so even .length might be of limited use for your use case.

Upvotes: 1

Related Questions