Issue with Nestjs and Jest: expect().toHaveReturnedWith() not returning any value

Currently experimenting with nestjs and jest in a personal project, I've encountered some challenges while trying to properly utilize the expect().toHaveReturnedWith() methods.

I wonder if the issue lies in how I set the resolved value:

jest.spyOn(subscriptionsHistoryRepository, 'findAll').mockResolvedValue([mockRecords, 2])
? Although upon debugging the test, it seems that .findAll() does return the mockRecords as expected.

Here is the snippet of code I am working on:

describe('SubscriptionsModule', () => {
  describe('GetSubscriptionHistoryService', () => {
    let service: GetSubscriptionHistoryService;
    let subscriptionsHistoryRepository: SubscriptionsHistoryRepository;

    beforeAll(async () => {
      const moduleRef: TestingModule = await Test.createTestingModule({
        providers: [
          GetSubscriptionHistoryService,
          {
            provide: SubscriptionsHistoryRepository,
            useValue: {
              findAll: jest.fn(),
            },
          },
        ],
      }).compile();

      service = moduleRef.get<GetSubscriptionHistoryService>(GetSubscriptionHistoryService);
      subscriptionsHistoryRepository = moduleRef.get<SubscriptionsHistoryRepository>(
        SubscriptionsHistoryRepository,
      );
    });

    afterEach(() => {
      jest.resetAllMocks();
    });

    it('Should have all class instances defined', () => {
      expect(service).toBeDefined();
      expect(subscriptionsHistoryRepository).toBeDefined();
    });

    it("Should return the subscription's status history", async () => {
      jest.spyOn(subscriptionsHistoryRepository, 'findAll').mockResolvedValue([mockRecords, 2]);

      await service.execute(CUSTOMER_ID, SUBSCRIPTION_ID, 1);

      expect(subscriptionsHistoryRepository.findAll).toHaveBeenCalledTimes(1);
      expect(subscriptionsHistoryRepository.findAll).toHaveBeenCalledWith({
        where: {
          customerId: CUSTOMER_ID,
          subscriptionId: SUBSCRIPTION_ID,
        },
        take: 5,
        skip: 0,
        order: {
          createdAt: 'DESC',
        },
      });
      expect(subscriptionsHistoryRepository.findAll).toHaveReturnedWith([mockRecords, 2]);
    });
  });
});

Test Results:

expect(jest.fn()).toHaveReturnedWith(expected)

Expected: [[{"createdAt": 2023-11-30T02:02:51.982Z, "customerId": "f1e8fc48-4780-45e4-858b-9e15b3382db5", "deletedAt": null, "extProviderStatus": "trialing", "id": "2e4c42b1-3d46-4438-bc68-64de46aa16d0", "status": "started", "subscriptionId": "2e4c42b1-3d46-4438-bc68-64de46aa16d5", "updatedAt": 2023-11-30T02:02:51.982Z}, {"createdAt": 2023-11-30T02:02:51.982Z, "customerId": "f1e8fc48-4780-45e4-858b-9e15b3382db5", "deletedAt": null, "extProviderStatus": "active", "id": "2e4c42b1-3d46-4438-bc68-64de46aa16d1", "status": "active", "subscriptionId": "2e4c42b1-3d46-4438-bc68-64de46aa16d5", "updatedAt": 2023-11-30T02:02:51.982Z}], 2]

Received: {}

All other assertions seem to be functioning correctly, but the line

expect(subscriptionsHistoryRepository.findAll).toHaveReturnedWith([mockRecords, 2])
returns an empty object {}.

Upon debugging the test run, it appears that spyOn.mockResolved worked fine. I'm uncertain about what could be missing or why this particular assertion is failing.

Answer №1

The issue at hand is that the function

subscriptionsHistoryRepository.findAll
is returning a promise.

This causes a problem when trying to compare a promise with an array, especially since toHaveReturnedWith uses strict equality (===) to check for the exact same result.

expect(subscriptionsHistoryRepository.findAll).toHaveReturnedWith([mockRecords, 2]);

To resolve this, you can create an expected promise and mock the function to return it like so:

const expectedPromise = Promise.resolve([mockRecords, 2]);
jest.spyOn(subscriptionsHistoryRepository, 'findAll').mockValue(expectedPromise);
[...]
expect(subscriptionsHistoryRepository.findAll).toHaveReturnedWith(expectedPromise);
    

I must admit that I haven't encountered a scenario where I needed to use toHaveReturnedWith in a unit test before. In this case, I believe that the assertion

expect(subscriptionsHistoryRepository.findAll).toHaveBeenCalledWith([...])
should suffice for testing purposes.

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

"Encountered a TypeScript error (2345) when using customElements.define() in Lit 2

When we upgraded various lit-Components to version 2.1.1 "lit": "2.1.1", a TypeScript error surfaced: The argument 'typeof MyComponent' cannot be assigned to a parameter of type 'CustomElementConstructor'. The &apo ...

What could be causing the getTotals() method to malfunction?

I have been working on a finance app that is designed to update the "income", "expenses", and "balance" tables at the top each time a new item is added by the user. However, the current code seems to be failing in updating these values correctly based on u ...

What is the process of transforming an array of string variables into TypeScript types?

I am inquiring about the process of converting an array of string variables to TypeScript types. It is worth noting that there are two existing SO answers addressing this issue, but they involve passing strings hardcoded to maintain their original values. ...

A method for modifying the key within a nested array object and then outputting the updated array object

Suppose I have an array called arr1 and an object named arr2 containing a nested array called config. If the key in the object from arr1 matches with an id within the nested config and further within the questions array, then replace that key (in the arr1 ...

JavaScript's Blob to Base64 conversion using FileReader is failing to produce any output

In my typescript application, I am utilizing the FileReader to convert a blob into a base64 image for display within the template. adaptResultToBase64(res: Blob): string { let imageToDisplay : string | ArrayBuffer | null = ''; const re ...

What could be causing my NextAuthJS discord login to function in the test environment but fail to deploy in production on NextJS 13?

After attempting to deploy my NextJS application on Vercel, I encountered a problem with the AuthJS sign-in model. While running the program using npm run dev, everything works smoothly and I can log in to my Discord account linked to the developer portal& ...

The SunEditor onChange event does not reflect updated state information

software version next: 12.0.7 suneditor: 2.41.3 suneditor-react: 3.3.1 const SunEditor = dynamic(() => import("suneditor-react"), { ssr: false, }); import "suneditor/dist/css/suneditor.min.css"; // Import Sun Editor's CSS Fi ...

In Typescript, how can we reverse the order of an enum

I am encountering a particular challenge with the following code snippet: enum MyEnum { Colors = 'AreColors', Cars = 'AreCars', } const menuTitle = ((obj: MyEnum) => { const newObj = {}; Object.keys(obj). ...

Removing the id attribute in the innerHTML property of Angular 4

After making an API call to load HTML content into an element by updating the innerHTML, everything seems to be working fine except for one issue - the id attribute gets removed from the loaded content. Here is the component code: content: string; @Vie ...

Passing properties from a parent component to a child component in a React TypeScript application

I'm currently facing an issue with passing props to my component. It seems like I am unable to pass the 'Commune' due to it having a name property. Does anyone have any suggestions on how I can pass Commune.name as a prop to my component? ...

The attribute 'size' is not recognized within the data type 'string[]' (error code ts2339)

When using my Windows machine with VSCode, React/NextJS, and Typescript, a cat unexpectedly hopped onto my laptop. Once the cat left, I encountered a strange issue with my Typescript code which was throwing errors related to array methods. Below is the co ...

JavaScript library declaration files are essential for providing type definitions and enabling

I have encountered a problem with my JS library and its declaration files (*.d.ts) in my TypeScript projects. For some reason, my TS project seems to be ignoring these declaration files. To investigate this issue further, I decided to conduct a simple tes ...

Steps for modifying the look of a button to display an arrow upon being clicked with CSS

Looking to enhance the visual appearance of a button by having an arrow emerge from it upon clicking, all done through CSS. Currently developing a React application utilizing TypeScript. Upon clicking the next button, the arrow should transition from the ...

Synchronize Docker volumes

Hey there! I've been trying to dive into learning Docker, but I'm having trouble syncing the host and container using volumes when making changes and saving code (specifically using npm run dev). Every time I need to restart docker-compose up --b ...

Discover the method of extracting information from an object and utilizing it to populate a linechart component

Object Name: Upon calling this.state.lineChartData, an object is returned (refer to the image attached). The structure of the data object is as follows: data: (5) [{…}, {…}, {…}, {…}, {…}, datasets: Array(0), labels: Array(0)] In the image p ...

MongoDB NextJS connection issue "tried to retrieve a connection from a closed connection pool"

I am attempting to establish a connection to my MongoDB database in order to retrieve some information. When setting up the connection without fetching any data, everything works fine. However, when trying to fetch data, the console throws this error: at ...

Is it necessary for Angular Reactive Form Validator to convert types before checking the value for min/max validation?

Preface: My motivation for asking the questions below stems from my experience with form.value.purchaseCost. When the <input> field does not have type=number, I receive a string instead of a number. This required me to manually convert it to Number ...

How to configure mat-sort-header in Angular Material for mat-table

My current table is created using Angular Material: <mat-table *ngIf="!waiting" class="table-general table-summary" #table [dataSource]="dataSource" matSort> <mat-header-row class="header_row" *matHeaderRowDef="headerKeys"></mat-header ...

The AutoComplete feature of MaterialUI Component fails to function properly even when there is available data

I am facing an issue with my component as it is not displaying the autosuggestions correctly. Despite having data available and passing it to the component through the suggestions prop while utilizing the Material UI AutoComplete component feature here, I ...

Stop repeated form submissions in Angular using exhaust map

How can we best utilize exhaust Matp to prevent multiple submissions, particularly when a user is spamming the SAVE button? In the example provided in the code snippet below, how do we ensure that only one submission occurs at a time even if the user click ...