Mr Boss
Mr Boss

Reputation: 769

Testing Number.prototype with Mocha

Let's say I have a function on Javascript's Number prototype as follows:

controllers/index.js

Number.prototype.adder = function(num) {
    return this+num;
}
module.exports = Number;

However, the following Mocha/Chai tests are failing

var expect= require("chai").expect;
var CustomAdder= require("../controller/index.js");
describe("adder", function () {
    var one= 4;
    var two= 5;

    it("should add 4 and 5 to 9", function(done){
        expect(one.CustomAdder(5)).to.equal(9);
        done();
    });


    it("should not add 5 and 6 to 11", function(done){
        expect(two.CustomAdder(6)).to.not.equal(12);
        done();
    });

});

Error: TypeError: undefined is not a function

I am pretty sure that the problem is caused by the module.exports = Number part. So basically my question is- how do I export a function in Number.prototype to make it testable as above.

Upvotes: 0

Views: 862

Answers (1)

Jimmy
Jimmy

Reputation: 91

Your function is called adder, so you should do

expect(one.adder(5)).to.equal(9);

Instead of

expect(one.CustomAdder(5)).to.equal(9);    

Upvotes: 1

Related Questions