Javascript 在nodejs中拦截外部API调用

Javascript 在nodejs中拦截外部API调用,javascript,node.js,mocha.js,chai,nock,Javascript,Node.js,Mocha.js,Chai,Nock,js),我想使用mocha和chai正常测试它,但是API与我想模拟的外部服务有依赖关系。是否可以模拟内部调用 按照下面的示例进行操作,以清楚地了解情况: describe('account Link', function() { it('should return with account status', async function(done) { nock('http://api.github.com', { allowUnmocked: true })

js),我想使用mocha和chai正常测试它,但是API与我想模拟的外部服务有依赖关系。是否可以模拟内部调用 按照下面的示例进行操作,以清楚地了解情况:

describe('account Link', function() {
    it('should return with account status', async function(done) {
      nock('http://api.github.com', { allowUnmocked: true })
      .get('/users/AKGP')
      .reply(200, 
        {
          test: true
        });

     const options = {
       uri: 'http://localhost:3000/internal_api',
       body: {},
       headers: {
         'content-type': 'application/json'
       },
       method: 'POST',
       json: true
     }
     try {
      const res = await rp(options)
      console.log(res)
      done();
     } catch(err){
       console.log(err);
       done();
     }
    });
  });

// the nocked api is called internally and I am expecting to mock it (http://api.github.com)

你所做的应该有用。nock也可以模拟内部调用。您是否确保您的请求与其他nock不会模拟的请求相匹配?是的,是相同的api调用