Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if something is a function with Chai.should

I want to check if something is a function with Chai.should. So I did

typeof(barfoo).should.equals('function')

DEMO

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

like image 216
Jeanluca Scaljeri Avatar asked May 29 '16 09:05

Jeanluca Scaljeri


People also ask

How to test a function in Mocha?

Running this command instructs Mocha to attach a special run() callback function to the global context. Calling the run() function instructs it to run all the test suites that have been described. Therefore, run() can be called after the asynchronous operation is completed to run the tests.

What is chai for testing?

Chai is an assertion library that is mostly used alongside Mocha. It can be used both as a BDD / TDD assertion library for NodeJS and can be paired with any JavaScript testing framework. It has several interfaces that a developer can choose from and looks much like writing tests in English sentences.

What is done () in Chai?

notify(done) is hanging directly off of . should , instead of appearing after a promise assertion. This indicates to Chai as Promised that it should pass fulfillment or rejection directly through to the testing framework.

Should I assert vs vs expect?

Note expect and should uses chainable language to construct assertions, but they differ in the way an assertion is initially constructed. In the case of should , there are also some caveats and additional tools to overcome the caveats. var expect = require('chai').


1 Answers

Here is how you can check if barfoo is a function:

barfoo.should.be.a('function');
like image 137
Do Async Avatar answered Sep 19 '22 17:09

Do Async