Mocking API calls in my test suite: fails when running in Drone, works local

I have a frontend that I’m testing using some testing framework. I use npmjs.org/packages/nock to do mocking. This works great when running my tests locally on my laptop.

When I push my code, and Drone runs my tests, I am presented with a “ECONNREFUSED” for http://localhost:8000 (which is my API endpoint that I’m mocking).

So for some reason, my mocking is failing on Drone/Docker. I have ran my tests on my laptop, on a brand new server (with same ENV variables as Drone), and on a colleague’s laptop and they all worked. Is there anything I’m missing here? This the test logic that fails inside my Docker pipeline:

// Testing only generated HTML
test('Route / exits and render HTML', async t => {
    nock(process.env.CMS_URL) // localhost:8000
        .get('/api/v1/cities')
        .reply(200, {
            cities: []
        })
    let context = { req: {} }
    const { html } = await t.context.nuxt.renderRoute('/', context)
    t.true(html.includes('Hello World'))
})

this is the error inside my Drone logs:

  ✖ Route / exits and render HTML Rejected promise returned by test
{ Error: connect ECONNREFUSED http://localhost:8000
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8000,
  config: 
   { adapter: [Function: httpAdapter],
     transformRequest: { '0': [Function: transformRequest] },
     transformResponse: { '0': [Function: transformResponse] },
     timeout: 0,
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus],
     headers: 
      { Accept: 'application/json, text/plain, */*',
        'User-Agent': 'axios/0.17.1' },
     method: 'get',
     url: 'http://localhost:8000/api/v1/cities',
     data: undefined }

I’ve already tried changing URLs to 0.0.0.0:8000 or 127.0.0.1:8000, but that didn’t help: same error. It simply should not even be trying to connect to that URL, because I’m intercepting it with my mocking.

I am happy to provide some tips, but would need a complete example / steps that can be used to reproduce the problem.

Thanks, I’ll work on a github repo where I can reproduce this.

I am having a very similar problem here.

Steps to reproduce:

We are using this drone config to run some tests that include Nock calls.

pipeline:
  build:
    image: node:8-alpine
    commands:
      - apk add --no-cache openjdk8-jre
      - npm ci
      - npm test
    when:
        event: [ push, pull_request ]
branches: master

At one point in the test script we need to connect to http://${hostname}:${port}. Without network_mode: host hostname is a 169.X IP, with network_mode: host it is a 10.X IP address. We have also tried to hardcode localhost and 127.0.0.1 as the hostname without success. All the tests run just fine when we run in a docker container on a local machine so my assumption is that this is some networking problem in the container created by drone. If you have any tips on how to proceed I would be very grateful.

Update: This was because of a segmentation fault in java running on node:8-alpine. We are still debugging the alpine environment, but verified that node:8 debian environment works just fine.