Test using Nock in a scheduleJob

我是研究僧i 提交于 2019-12-24 22:46:03

问题


I'm getting a problem with test the http request in a scheduleJob node. Well, I've changed the crontime to run every second as suggested here for easier testing. The problem is, I don't think the Nock that I created to test the http request works fine. Because when I do the console.log('res : ', res); it either returns undefined or does not print anything as shown here : enter image description here. Does anyone have any idea how to solve this?

Index.js

schedule = require('node-schedule'),
var dateObj = Date.now();
dateObj += 1000;
schedule.scheduleJob(new Date(dateObj), function(){
console.log("get's here..");
const url_req = "https://git.ecommchannels.com/api/v4/users";
request.get(
    {
     url_req,
     qs : {private_token: token, per_page: 10}
}, function(err, res, body){
    console.log('res : ', res);
    var total = res.headers['x-total-pages']
    console.log('res.headers', res.headers);
    for( i=1; i<=total; i++ ){
        url_req2 = "https://git.ecommchannels.com/api/v4/users";
        request.get({
            url_req2,
            qs: {private_token : token, per_page : 10, page : i},
            json : true
        },
            (err2, res2, body2) => {
            body2.forEach(element => {
                if(condition){
                    //do something
                }
            })
        })
    }
})

});

The unit test:

describe('test the http request', function(){
var clock;
const nockingGit = () =>{
    nock('https://git.ecommchannels.com')
    .defaultReplyHeaders({
        'x-total-pages': 10
    })
    .get('/api/v4/users')
    .query({private_token: 'blabla', per_page: 10})
    .reply(201, {});
}
beforeEach(function () {
  clock = sinon.useFakeTimers();
});

afterEach(function () {
  clock.restore();
});
it('test the http request', (done)=>{
    nockingGit();
    setTimeout(function(){
        done();
    },3000)
    clock.tick(3000);
})

})


edited

index.js

schedule.scheduleJob(new Date(dateObj), function(){
  var now = new Date();
  flag = true;
  console.log(now.toLocaleString('en-US', { timeZone: 'Asia/Jakarta'}));
  console.log("get's here..");
  gitReq();
});

function gitReq(){
const url_req = "https://git.ecommchannels.com/api/v4/users";
request.get(
  {
     url_req,
     qs : {private_token: token, per_page: 10}
  }, function(err, res, body){
    console.log('res : ', res);
    var total = res.headers['x-total-pages']
    // console.log('res.headers', res.headers);
    for( i=1; i<=total; i++ ){
        url_req2 = "https://git.ecommchannels.com/api/v4/users";
        request.get({
            url_req2,
            qs: {private_token : token, per_page : 10, page : i},
            json : true
        },
            // "https://git.ecommchannels.com/api/v4/users?private_token=" + token + "&per_page=10&page=" + i, { json: true }, 
            (err2, res2, body2) => {
            body2.forEach(element => {
                if(condition){
                    //do something
                }
            })
        })
    }
  })
}

Unit Test

*check whether the gitReq() is called - using sinon.spy (but I'm not sure whether the implementation is correct)

const sendReq = {
gitReq: ()=>{}}

describe('test the schedule job', function(){
var clock;
beforeEach(function () {
  clock = sinon.useFakeTimers();
});

afterEach(function () {
  clock.restore();
});
it('should call the gitReq once', (done)=>{
    let gitReq = sinon.spy(sendReq,"gitReq");
    sendReq.gitReq();
    console.log("callcount : ", gitReq.callCount);
    gitReq.restore();
    setTimeout(function(){
        expect(gitReq.calledOnce).to.be.true;
        done();
    },3000)
    clock.tick(3000);
})})

*test the http request using nock that still returns undefined for the res.

describe('test the http request', function(){
  const nockingGit = () =>{
    nock('https://git.ecommchannels.com')
      .defaultReplyHeaders({
        'x-total-pages': 10
      })
      .get('/api/v4/users')
      .query({private_token: 'UKz2cZP9CHz2DT_o2-s9', per_page: 10})
      .reply(304, {});
  }
  it('test the http request', function(){
    nockingGit();
    _import.gitReq()
  })
})

*here's the test result

来源:https://stackoverflow.com/questions/58951664/test-using-nock-in-a-schedulejob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!