What is the method for declaring an async callback function as a response in Nock?

I am facing an issue with a nock interceptor that is intercepting calls from an async function (which returns a promise)

public async backendRes(): Promise<container> {
  get some data from the backend 
  return new container(stat, body, header);
}

When I call it using nock like this:

nock()
  .get('/goodey')
  .reply(async function () {
    let abc = await global.MyClass.backendRes();
    return [abc.getStatus(), abc.getBody(),
      abc.getHeader()];
  });

Unfortunately, this setup is not working correctly. The reply within the nock seems to be incorrect, but I'm unsure how to resolve it. Any assistance would be greatly appreciated.

Answer №1

The current nock documentation fails to mention the support for async / Promise-returning functions in the reply() method. It only states that callback arguments or node.js streams are supported.

If you prefer, you can utilize the callback mechanism by passing it as a third argument:

nock()
  .get('/goodey')
  .reply(async function (uri, requestBody, cb) {
    let abc = await global.MyClass.backendRes();
    cb(null, [abc.getStatus(), abc.getBody(),
      abc.getHeader()];
    return;
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The CORS policy has blocked access to 'http://localhost:8080/BeginSignup' from 'http://localhost:4200'

I'm having trouble with a CORS policy error when sending a fetch POST request to my Golang AppEngine API. Although I don't understand why this error is occurring. Below is the code I'm using: Below is the Angular code calling the API: priva ...

Multer in NextJS having trouble uploading complete files (stopping at 195KB)

My objective involves uploading image and video files to the /public/uploads directory through a form on my Index.tsx. I am utilizing Multer in my API Endpoint to accomplish this task. Unfortunately, a peculiar issue arises when attempting to upload files ...

Retrieve data from a web api at regular intervals using Angular observables and subscription

My Launch method is designed to start an engine by taking parameters and returning the instance name once started. After that, I need to periodically query another service every 2 seconds to check if the status has changed to either "Succeeded" or "Faile ...

Tips for configuring the Index column within an Angular Mat-table (when the dataIndex displays 'NaN')

My Mat-Table is working perfectly, but I am looking for a way to add an auto-increment index. Below is the HTML code: <div class="mat-elevation-z8"> <table mat-table [dataSource]="dataSource" matSort> <ng-container matColumnDef="no"> ...

Examining the creation of mongoose model instances with Jest

Exploring the functionality of a REST API created with express and mongoose, I am utilizing jest and supertest for handling http calls. Despite being new to testing in javascript, I am eager to learn. Specifically when testing a creation URL, my goal is t ...

I attempted to execute an art configuration on Metaplex, but I keep encountering the same issue. Despite numerous attempts to modify various settings, the problem persists

Error encountered while compiling TypeScript code on desktop using ts-node to generate art configuration traits: Unable to find module 'commander' or its corresponding type declarations. Unable to find module '@project-serum/anchor' or ...

Typescript: Transforming generic types into concrete types

I am utilizing a Generic type type GenericType = { [key: string]: { prop1: string, prop2?: string, prop3?: number, }, }; The purpose of the Generic type is to assist in constructing / validating a new object that I have created. const NewO ...

Implementing Conditional Validation Triggers in Angular

My app is built using Angular 8. It features a form where users can input movie details and ticket information. Data Model Setup: export class Movie { name:string; } export class Ticket { name:string; price:number; } To create the form, I am ut ...

Navigate to a particular date, month, or year on fullCalender using react-datepicker

Currently, I have integrated the fullCalendar and react-datepicker library into my React application. Here are the versions I am using: "react": "^17.0.1", fullCalendar version: "@fullcalendar/core": "^5.9.0", "@fullcalendar/daygrid": "^5.9.0", "@fullcale ...

Implementing an external abstract class in Typescript without inheriting from it

In my code, there is a module named utils.tsx defined as follows: interface IUtils { toUri: (route: string) => string } export default abstract class Utils implements IUtils { public toUri = (route: string) => { return route } } ...

Confirm the array of elements

Looking to validate an array of objects being sent through Postman, structured like this: { pit_fee: [ { range: [1,2] fee:25 }, { range: [3,4] fee:30 }, } Using the following validation schema with class-validators: export class ...

How can you define a generic type alias in TypeScript for a function that is also generic?

I am facing a challenge with creating a generic alias for a typed generic function. Despite my efforts, I have not been successful in achieving this. Here is an example of what I'm encountering: // foo.tsx function foo<T>(arg: T): T { return a ...

What is the best way to transfer a variable between components in Angular without using the HTML page, directly within the components themselves?

Within the child component, I am working with a string: @Input() helloMessage:string; I am looking to assign a value to this string from another string in the parent component and utilize it within the child component without displaying the value in the h ...

What is the reason behind the return type of 'MyType | undefined' for Array<MyType>.find(...) method?

Currently, I am in the process of developing an Angular application using TypeScript. As part of this project, I have defined several classes along with corresponding interfaces that align perfectly with their respective properties: Map: export class Map ...

Can you apply transparency to a hex color variable in SCSS and then use that variable again?

In my app, I have a set of scss variables that represent colors used throughout the interface. For example: $primary-color: #00755E There are also colors that are variations of this primary color with different opacities. For instance, $primary-color with ...

Exploring the concept of type inheritance in TypeScript

I am working on developing various components for an app, each with its own specific structure. The general structure is defined as COMPONENT. Within this framework, there are two distinct components: HEADING and TEXT. These components should be subclasses ...

Converting retrieved data into table cells through mapping

I'm facing an issue where I need to display specific addresses for each individual in a table row. For simplicity, let's focus on showing the city specifically as described in this code snippet: https://i.stack.imgur.com/bJmsD.png Currently, whe ...

What causes an inference site to have varying effects when accessed directly versus when it is retrieved from a function?

Below is the provided code snippet : declare class BaseClass<TValue = any> { value: TValue; foo(value: TValue): void; } type Wrapped<T> = { value: T } declare class ConcreteClasss<TValue> extends BaseClass<TValue> { construc ...

Highlighting DOM elements that have recently been modified in Angular

Is there a simple way to change the style of an element when a bound property value changes, without storing a dedicated property in the component? The elements I want to highlight are input form elements: <tr field label="Lieu dépôt"> ...

Is there a way to revert my Ionic CLI back to the previous version that I had installed?

Having just updated to version 3.2.0, I am encountering numerous issues, such as the malfunctioning of the ionic serve command. ...