Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs unit test error "sinon.restore is not a function"

Hey there I am trying to write unit tests by using sinon ,however, I am not able to reset sinon changing behaviour of my stub. First I was having

TypeError: Attempted to wrap getLastData which is already wrapped

After some research I got that I need to reset sinon but I am getting this sinon.restore is not a function error. Additionally, QueryHelper.getLastData() just returns a promise which resolves by returning an entity like lastData

var assert = require('assert');
var sinon = require('sinon');
var proxyquire = require('proxyquire');
var ExchangeHandlerFactory = require('../handler.js');
var QueryHelper = require('../query-helper.js');

describe('BinanceHandler', function() {
    var binanceHandler;
    var config;

    before(function() {
        config = {
            exchange: 'binance',
            interval: '1h'
        };
        var ExchangeHandlerFactoryObj = proxyquire('../handler.js', 
                                            {"./config.js": config,
                                                "./query-helper.js": QueryHelper});
        binanceHandler = ExchangeHandlerFactoryObj.getExchangeHandler('binance', '1h')
    });

    afterEach(() => {
        QueryHelper.getLastData.restore();
    });

    describe('#buildGetMarketTickerUrl()', function() {
        it('binance handler should return correct buildGetMarketTickerUrl', async function() {
            var lastData = {
                            symbol: "BTC-VTC",
                            interval: "1h",
                            exchange: "binance",
                            dataSet: [
                                {
                                    "o" : 0.0006882,
                                    "h" : 0.00071,
                                    "l" : 0.0006882,
                                    "c" : 0.0007008,
                                    "bv" : 63.27676876,
                                    "t" : 1524963600000
                                }
                        ]};
            sinon.stub(QueryHelper, "getLastData")
                .withArgs({exchange: "binance", interval: "1h", symbol: "BTC-VTC"})
                .resolves(lastData);
            var marketUrl = await binanceHandler.buildGetMarketTickerUrl("VTC");
            assert.equal(marketUrl, 
                         "https://api.binance.com/api/v1/klines?symbol=VTCBTC&interval=1h&startTime=1524963600000");
        });
    });
});

NOTE: I also tried QueryHelper.getLastData.restore() in afterEach() block. But then I am getting QueryHelper.getLastData.restore is not a function error.

Thanks a lot for your help.

like image 861
quartaela Avatar asked Dec 17 '22 23:12

quartaela


1 Answers

I resolved this by first creating stub of QueryHelper in beforeEach() hook. Here is the fix I did.

beforeEach(function() {
    queryHelperStub = sinon.stub(QueryHelper, "getLastData");
});

afterEach(() => {
    queryHelperStub.restore();
});

describe('#buildGetMarketTickerUrl()', function() {
    it('binance handler should return correct buildGetMarketTickerUrl', async function() {
        var lastData = {
                        symbol: "BTC-VTC",
                        interval: "1h",
                        exchange: "binance",
                        dataSet: [
                            {
                                "o" : 0.0006882,
                                "h" : 0.00071,
                                "l" : 0.0006882,
                                "c" : 0.0007008,
                                "bv" : 63.27676876,
                                "t" : 1524963600000
                            }
                    ]};
        queryHelperStub.withArgs({exchange: "binance", interval: "1h", symbol: "BTC-VTC"})
                        .resolves(lastData);
        var marketUrl = await binanceHandler.buildGetMarketTickerUrl("VTC");
        assert.equal(marketUrl, 
                     "https://api.binance.com/api/v1/klines?symbol=VTCBTC&interval=1h&startTime=1524963600000");
    });
});

describe('#buildGetMarketTickerUrl()', function() {
    it('binance handler should return correct buildGetMarketTickerUrl with startTime and endTime params', async function() {
        queryHelperStub.withArgs({exchange: "binance", interval: "1h", symbol: "BTC-VTC"})
                        .resolves(null);
        var marketUrl = await binanceHandler.buildGetMarketTickerUrl("VTC");
        assert.equal(marketUrl,
                     "https://api.binance.com/api/v1/klines?symbol=VTCBTC&interval=1h");
    });
});
like image 90
quartaela Avatar answered Jan 07 '23 12:01

quartaela