samedi 23 décembre 2017

nock handles fetch req but does not prevent http call

I am using nock to intercept a fetch call to https://www.youtube.com/oembed?url=https://youtu.be/m4hklkGvTGQ . Like so:

it(`properly responds to YouTube URL ${url}`, async () => {
    nock('https://www.youtube.com')
        .get('/oembed')
        .query(true)
        .reply(200, remoteResponse);
    const youTube = new YouTube(url);
    const response = await youTube.getResponse(); // this contains the fetch call
    expect(response).toEqual(expectedResponse);
    nock.restore();
});

I see that nock properly intercepts my fetch because

  • If I change the remoteResponse (see line 5, .reply(200, remoteResponse) my tests behave / fail as expected
  • If I change the intercepting url from https://www.youtube.com to http://ift.tt/2l0LBUX, nock tells me it did not catch a fetch request.

However, if I switch off my WiFi and run my tests I can see that my application warns me about a failed HTTP request:

request to https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=m4hklkGvTGQ failed, reason: getaddrinfo ENOTFOUND www.youtube.com www.youtube.com:443

Also, when I run my tests, they take about 1 second (which already seems long), when I turn of my WiFi, they are done immediately.

I wonder why there are still http requests

Aucun commentaire:

Enregistrer un commentaire