Reputation: 29129
I want to check if something is a function with Chai.should. So I did
typeof(barfoo).should.equals('function')
This results in
AssertionError: expected [Function] to equal 'function'
at Context.<anonymous> (:73:31)
Can someone explain to me why this doesn't work, because if I simply do
typeof(barfoo)
I get a string function
. Although I've solved this now with instanceOf
but I really want to understand this
Upvotes: 17
Views: 11191
Reputation: 2879
It's also possible to test with instanceOf:
barfoo.should.be.instanceOf(Function);
Upvotes: 5
Reputation: 4284
Here is how you can check if barfoo
is a function:
barfoo.should.be.a('function');
Upvotes: 36
Reputation: 46598
You need extra parentheses
(typeof(barfoo)).should.equals('function')
Otherwise your expression is interpreted as
typeof ((barfoo).should.equals('function'))
as you can see, you are calling .should
on barfoo
instead of typeof barfoo
and then get the type of final result and discard it.
This is because typeof
it not a function, it is an unary operator[1] and have a lower precedence than Member Access operator (.
) and Function Call[2]. So typeof
is evaluated last in this expression.
Upvotes: 8