【发布时间】:2017-12-23 14:31:18
【问题描述】:
我正在使用 nock 拦截对 https://www.youtube.com/oembed?url=https://youtu.be/m4hklkGvTGQ 的 fetch 调用。像这样:
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();
});
我看到nock 正确拦截了我的fetch,因为
- 如果我更改
remoteResponse(参见5、.reply(200, remoteResponse)行,我的测试会按预期运行/失败 - 如果我将拦截 url 从
https://www.youtube.com更改为https://www.sometingelse.com,nock会告诉我它没有捕获到获取请求。
但是,如果我关闭 WiFi 并运行测试,我可以看到我的应用程序会警告我 HTTP 请求失败:
对https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=m4hklkGvTGQ 的请求失败,原因:getaddrinfo ENOTFOUND www.youtube.com www.youtube.com:443
另外,当我运行我的测试时,它们大约需要 1 秒(这似乎已经很长了),当我打开我的 WiFi 时,它们会立即完成。
不知道为什么还有http请求
【问题讨论】:
标签: javascript testing fetch jestjs nock