Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
    })
});
like image 401
Modelesq Avatar asked Feb 04 '26 07:02

Modelesq


1 Answers

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');
like image 78
hackerrdave Avatar answered Feb 05 '26 20:02

hackerrdave