Search code examples
node.jsjasminejasmine-node

How do I spyOn private functions in a node module using jasmine-node?


app.js

function _private() {
    console.log( '_private' );
}

function public() {
    console.log( 'public' );
    _private();
}

module.exports = {
    public: public,
    _private: _private
};

spec/appSpec.js

describe( 'test', function() {
    it( 'will spy on _private', function() {
        var app = require( '../app' );
        spyOn( app, '_private' );
        app.public();
        expect( app._private ).toHaveBeenCalled();
    });
});

_private() is called, but the spy doesn't work and the test fails.

So as the question asks, how do I hook the spy up so that it knows that _private() was called? Or is this not possible?


Solution

  • You can call _private with this otherwise the function is not defined. try this:

    function public() {
        console.log( 'public' );
        this._private();
    }