Node JS testing examples with Mocha, Proxyquire, Assert and Sinon

it("it should map the options", function (done) {
    var objMappingServiceMapSpy = sinon.spy(objMappingServiceMock, "map");

    target.callPublic(reqMock, resMock).then(function () {
        try {
            assert(objMappingServiceMapSpy.withArgs("HttpServiceOptions", optionsMock).calledOnce);
            done();
        }
        catch (err) {
            done(err);
        }
    });
});

describe("when the response is a success", function () {
    beforeEach(function () {
        requestPromiseMock = sinon.stub().returns(
            new Promise(function (resolve, reject) {
                resolve("api data");
            })
        );

        target = proxyquire("api/services/factories/httpServiceFactory", {
            "request-promise": requestPromiseMock,
            "api/services/api/apiConfigService": apiConfigServiceMock
        });

        instance = target.make("frankie");
    });

    it("it should return the data", function (done) {
        instance.call(optionsMock).then(function (data) {
            try {
                assert.deepEqual(data, "api data");
                done();
            }
            catch (err) {
                done(err);
            }
        });
    });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s