mercredi 15 mai 2019

Why is nock js hijacking axios requests to local host? and How to stub out requests with nock?

I'm struggling to stub out a call with nock. I want to allow axios to hit my locally running server, but I want my server code to stub out external calls to 'https://security:password@store.myshopify.com/'. However, whenever I try to do this with nock I get the following error:

3) /returns
       response with http status code of 200:
     Error: connect ECONNREFUSED 127.0.0.1:8080
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)

An my JS code trying to test an endpoint:

const axios = require('axios');
const expect = require('chai').expect;
const nock = require('nock');

const httpClient = axios.create({baseURL: "http://localhost:8080"});

describe("/returns", function() {
  it("response with http status code of 200", async function() {

    const scope = nock('https://security:password@store.myshopify.com/')
      .get('/admin/api/2019-04/orders.json?name=123')
      .reply(200, {
        orders: [
          {
            created_at: "2019-04-25T12:41:37-04:00",
            email: "alice@test.com",
            order_number: 123,
            shipping_address: {country_code: 'US'}
          }
        ]
      })

    let response = await httpClient.post("/returns", {
      email: "alice@test.com",
      orderNumber: 123
    });

    expect(response.status).to.equal(200);
  });


});

  1. It appears that nock is trying to interfere with the request to my local server. Is that the case? If so, why -- and how can I prevent it from interacting with my local server? I want axios to be able to hit it.

  2. Am I not selecting the route to stub out correctly? I just want to block http requests to shopify for now.

Aucun commentaire:

Enregistrer un commentaire