Modelesq
Modelesq

Reputation: 5402

How to test return values on functions with Chai

I'm using Chai + mocha + karma for testing my js...

I've got a simple function that will take a number and add 2:

function name(str) {
    return str + ' has come online';
}

I receive a assertion error, AssertionError: expected [Function: add] to be a string but I'm not sure why since it is a string...

describe("Number", function() {
    it("Should return a string value", function() {
        expect(name).to.be.a('string');
    })
});

Upvotes: 2

Views: 11674

Answers (2)

DINA TAKLIT
DINA TAKLIT

Reputation: 8388

If you are using assert you can do it that way:

assert.typeOf(name('someting'),'boolean');

You can check the documentation for more details

Upvotes: 1

hackerrdave
hackerrdave

Reputation: 6706

The test is throwing an error because it is checking the function name itself, not the result of invoking the function. You would need to do:

expect(name('something')).to.be.a('string');

Upvotes: 12

Related Questions