mercredi 8 mars 2017

Jest returns "Network Error" when doing an authenticated request with axios

This seems a bit weird to me. I'm trying to test an actual (ie. real network) request with Jest.

These are the tested scenarios

  • Test an external API(fixer.io) with no headers <--- This works
  • Test an local API server with headers <--- This does NOT work
  • Test same local API with headers from node terminal <--- This works

What could be the reason behind this behavior? And what is the solution?

//This WORKS
test('testing no headers', () => {
  return axios.get('http://ift.tt/1UmIXTI')
        .then( res => console.log(res) )
});

//This DOES NOT work
test('testing no headers', () => {
  return axios.get('http://localhost:3000/users/4/profile', 
                      {headers:{authorization:`Bearer ${mytoken}`}})
        .then( res => console.log(res) )
});

//...

//Node Terminal
//This WORKS
> axios.get('http://localhost:3000/users/4/profile', 
                   {headers:{authorization:`Bearer ${mytoken}`}})
        .then( res => console.log(res) )

Aucun commentaire:

Enregistrer un commentaire