Jest Error: Matcher error: received value must be a promise

Viewed 25

I am writing unit tests for my NestJS application. However, I am getting into this error which I don't get how I can resolve. I am resolving the Promise as well.

My update function in service.ts looks like this:

  async update(uname: string, updateUserDto: UpdateUserDTO): Promise<Users> {
    const updateUser = await prisma.users.findUnique({
      where: {
        username: uname,
      },
    });

    if (!updateUser || updateUser.username !== uname) {
      throw new NotFoundException('User not found');
    }

    await prisma.tasks.updateMany({
      where: {
        userId: uname,
      },
      data: {
        userId: uname,
      },
    });
    return prisma.users.update({
      where: {
        username: uname,
      },
      data: {
        ...updateUserDto,
      },
    });
  }

And my testing file looks like this.

controller.spec.ts:

    describe('Update a user', () => {
      it('should update the username of a user', async () => {
        const updatedUser: UpdateUserDTO = {
          name: 'Hulk',
        };
        await expect(
          controller.update('wolverine', updatedUser)
        ).resolves.toEqual({
          username: 'wolverine',
          ...updatedUser,
        });
      });
    });
  });

Update mock in controller.spec.ts is like this:

            update: jest
              .fn()
              .mockImplementation((username: string, user: UpdateUserDTO) =>
                Promise.resolve({ username, ...user })
              ),

And my controller where I am using update is, controller.spec.ts

  @Patch(':uname')
  update(@Param('uname') uname: string, @Body() updateUserDto: UpdateUserDTO) {
    return this.usersService.update(uname, updateUserDto);
  }
0 Answers
Related