ApiClient test throw ECONNREFUSED

I’m trying to run a test with the ApiClient trait, but it throws the error ECONNREFUSED 127.0.0.1:4000.

This is the simple code I use:

'use strict'

const { test, trait } = use('Test/Suite')('Check status code')

trait('Test/ApiClient')

const homepageTest = {
  redirect: '/en/',
  name: 'Homepage',
  data: '/'
}

test('Homepage', async ({ client }) => {
  let response = await client
    .get(homepageTest.data)
    .header('Accept-Language', 'en-US,en;q=0.5')
    .end()

  response.assertRedirect(homepageTest.redirect)
})

I have also other tests using it and in the terminal they pass, it seems that only for the first test it doesn’t work.

Only the first test of that type, with get() and assertRedirect() doesn’t work, because if I remove them and run other tests which use post() and send(), they pass with no error.

Example of post:

'use strict'

const { test, trait } = use('Test/Suite')('Post Job')

trait('Test/ApiClient')
trait('DatabaseTransactions')

let response = await client
      .post('/api/job')
      .header('Origin', 'foo')
      .send(singleTest.data)
      .end()

    response.assertStatus(singleTest.status)
    response.assertJSONSubset({
      response: (singleTest.status === 200 ? 'success' : 'error')
    })

Can u share the code of the controller where it redirects?

Hi,
the redirect is made by a middleware called Language:

'use strict'

const Antl = use('Antl')

class Language {
  async handle ({ request, response, locale, params }, next) {
    // If isn't under a language folder, redirect it
    if (typeof params.lang === 'undefined') {
      const url = request.url()
      return response.redirect('/' + locale + url, false, 302)
    }

    // Call next to advance the request
    await next()
  }
}

module.exports = Language

in the routes, to make it work, I have:

Route.group(() => {
  Route.get('/', () => {}) // Just to activate the middleware
}).middleware('language')

Yeah but does redirect lands somewhere, since too many redirects will fail the request

Yes, if you go to http://localhost:3333, it will redirect you to http://localhost:3333/en/ (by the Middleware), with just one redirect.

@virk I tried to debug it a bit, I tried switching port of tests with TEST_SERVER_URL in my .env, but the server starts on the wrong port ignoring my .env, but tests will try to send data on the correct endpoint.

I tried with TEST_SERVER_URL=http://127.0.0.1:50054, and this is the output from DEBUG=* adonis test:

...
adonis:vow creating isolated response for the suite +2ms
  adonis:fold resolving Adonis/Src/Server namespace as a binding +1ms
2018-01-02T09:46:34.987Z - info: serving app on http://127.0.0.1:4000
  adonis:vow:runner running 1 trait(s) for Check status code suite +8ms
  adonis:fold resolving Test/ApiClient namespace as a binding +7ms
  adonis:vow:runner running 2 trait(s) for Post Job suite +2ms
  adonis:fold resolving Test/ApiClient namespace as a binding +1ms
  adonis:fold resolving Adonis/Traits/DatabaseTransactions namespace as a binding +0ms
  adonis:vow:runner executing tests +1ms

  Check status code
  adonis:vow instantiating api client +0ms
  superagent GET http://127.0.0.1:50054/ +0ms
    ✖ Homepage (29ms)

As you can see, it starts the server on 4000 (2018-01-02T09:46:34.987Z - info: serving app on http://127.0.0.1:4000), but then it try to send request to 50054 (superagent GET http://127.0.0.1:50054/ +0ms).

Maybe this could help.

This problem is caused by the .env.testing file, which is probably set on port 4000. Try to find some file with NODE_ENV=testing set on port 4000.