I am trying to upgrade my node project to use version 18 and the built in fetch vs our old method of using node-fetch. We were writing tests using nock, which isn’t an option with this version of fetch. I am not able to get undici to intercept the request. I am getting the response as if I went to the page (google.com/test)
I created a standalone test file and that didn’t work either.
const { MockAgent, setGlobalDispatcher, } = require('undici');
test('should respond with body passed in', async () => {
const opts = { method: 'GET' };
const body = { statusInfo: { status: 'SUCCESS' }};
const agent = new MockAgent({ connections: 1 });
agent.get('http://www.google.com').intercept({
path: '/test'
}).reply(200, body);
agent.disableNetConnect();
agent.activate();
setGlobalDispatcher(agent);
const result = await fetch('http://www.google.com/test', opts);
console.log(JSON.stringify(result));
// console.log('%o', result);
console.log(await result.text());
// expect(result).toEqual(body);
});