question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MockClient not intercepting the request

See original GitHub issue

I know that this is under development, so I am using the last release candidate version (ie v4.0.0-rc.7).

So, I am trying to unit test my undici code, and it doesn’t work as expected or there’s something that I didn’t understand correctly. I have a very simple ts file that sends a get request. It’s presented like this:

import { request } from 'undici'

import { APIResponse } from './apiresponse.interface'

export class clientAPI {

    private API_URL = 'https://example.com/tdd/everything';
    
    public async getCall() : Promise<APIResponse> {
        const { body } = await request(this.API_URL);
        for await (let data of body) {
            return <APIResponse> data
        }
    } 
}

and, so after many attempts, found a good documentation about unit testing the async code, so I wrote this in my test file:

import { MockAgent, MockClient } from 'undici';

import { ClientAPI } from "./client-api"
import { APIResponse } from "./api-response.interface";


describe("client-api.ts test", () => {

    it("Returns the Working parameter in a APIResponse type", async () => {
        
        // Given
        const origin = 'https://example.com'
        const mockAgent = new MockAgent({ connections: 1 });
        const mockClient = new MockClient(origin, {agent: mockAgent})
        const clientAPI = new ClientAPI()
        const expectedResponse = <APIResponse>{Working:1}

        mockClient.intercept({
            path: '/tdd/everything',
            method: 'GET'
        }).reply(200, expectedResponse)
        
        // When
        const response = await clientAPI.getCall()

        // Then
        expect(response).toBe(expectedResponse)
    });


})

So far, the request is not intercepted and the mocking system is not working, because in my test run I can see the API response ( not what I put in the expectedResponse object ). Also, using @mcollina code was giving me an error that said Dispatcher has no method intercept, that’s why I adapted the mockClient instanciation.

Thanks for your help and GJ for the opensource contribution, you guys rock !!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
GO3LINcommented, Jun 9, 2021

I think casting to MockClient helps to intercept the request, but then, I got another error: TypeError: this[kAbort] is not a function

And here’s how I casted the dispatcher to a MockClient:

const mockClient = <MockClient>mockAgent.get(origin)

0reactions
throrin19commented, Jan 13, 2022

Same problem here with same type of code. I have the error TypeError: this[kAbort] is not a function

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nock is not intercepting the request in my test - Stack Overflow
The issue was housing this in the function const productNock = (productId) => {. Just having this with a stubbed productId in my...
Read more >
mocking outbound http requests in go: you're (probably) doing ...
A simple scenario. To illustrate, suppose we started working on a codebase and found the following untested function, and now we want to...
Read more >
Mocking With Undici Like a Pro - Fusebit
By default, MockAgent will pass through any calls it cannot match via an intercept() method into real HTTP requests, which can make debugging ......
Read more >
Mock API Generation with Twilio's OpenAPI Spec
This mock server can receive requests, and will respond to them with static or dynamic data that simulates what the real API would...
Read more >
Mock Client — PHP-HTTP 1.0.0 documentation
It is a test double that does not send the requests that you pass to it, but collects them instead. You can then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found