我正在使用新的(从版本18开始)Node.js“fetch”API来执行HTTP请求,例如:
const response = await fetch(SOMEURL)
const json = await response.json()
这是可行的,但是我想“模拟”那些HTTP请求,以便我可以进行一些自动化测试,并能够模拟一些HTTP响应,以查看我的代码是否正常工作。
通常,我使用Axios和优秀的nock包来模拟HTTP请求,但它似乎不适用于节点18中的fetch
。
那么,在Node.js中使用fetch
时,如何模拟HTTP请求和响应呢?
1条答案
按热度按时间mzillmmw1#
Node 18's
fetch
function isn't built on the Node.js http module like most HTTP libraries (including Axios, request etc) - it's a total rewrite of an HTTP client built on the lower-level "net" library called undici . As such, "nock" cannot intercept requests made from thefetch
function (I believe the team are looking to fix this, but at the time of writing, Nock 13.2.9, Nock does not work forfetch
requests).The solution is to use a MockAgent that is built into the undici package.
Let's say your code looks like this:
This code makes a real HTTP request to a CouchDB server running on localhost.
To mock this request, we need to add
undici
into our project:and add some code to intercept the request:
The above code now has its HTTP "fetch" request intercepted with mocked response.