const { MockAgent, setGlobalDispatcher, Pool } = require('undici')
const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)
const mockPool = mockAgent.get('https://jsonplaceholder.typicode.com')
mockPool.intercept({
path: '/todos/1',
method: 'GET'
}).reply(200, 'foo')
async function run () {
try {
const agent = new Pool('https://jsonplaceholder.typicode.com', {
headersTimeout: 30e3, // 30 seconds
maxHeaderSize: 16_384, // 16 KiB
keepAliveMaxTimeout: 5000, // 5 seconds
connections: 50,
tls: { rejectUnauthorized: false },
})
const { statusCode, body } = await agent.request({
path: '/todos/1',
method: 'GET',
})
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
}
catch (error) {
console.log(error)
}
}
run()
Check this example based on these docs https://undici.nodejs.org/#/docs/api/MockPool?id=example-basic-mockpool-instantiation
When
The request never gets intercepted, not sure if I am missing something here