Home > Net >  Mock bcrypt module module in Nest.js
Mock bcrypt module module in Nest.js

Time:01-09

I'm trying to mock bcrypt hash method implementation, but get following error:

Error: thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

I've tried to increase timeout up to 30000. Also I've tried to mock entire bcrypt module like jest.mock('bcrypt'). I'm new to testing and there are may be some logical errors or bad practises. I will be grateful if you point to them.

My code:

import { UserService } from '../user.service';
import { Test, TestingModule } from '@nestjs/testing';
import { Repository } from 'typeorm';
import { getRepositoryToken } from '@nestjs/typeorm';
import * as bcrypt from 'bcrypt';

import { UserEntity } from '../user.entity';
import { UserRepository } from '../user.repository';
import { CreateUserDto } from '../dto/create-user.dto';
import { userStub } from './stubs/user.stub';

describe('UserService', () => {
  let userService: UserService;
  let userRepository: Repository<UserEntity>;

  beforeAll(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        UserService,
        {
          provide: getRepositoryToken(UserRepository),
          useClass: Repository,
        },
      ],
    }).compile();

    userService = module.get<UserService>(UserService);
    userRepository = module.get<Repository<UserEntity>>(
      getRepositoryToken(UserRepository),
    );
  });

  it('should define UserService', () => {
    expect(userService).toBeDefined();
  });

  it('should define userRepository', () => {
    expect(userRepository).toBeDefined();
  });

  describe('createUser method', () => {
    it('has called with valid data', async () => {
      const createUserDto: CreateUserDto = {
        email: userStub().email,
        firstName: userStub().firstName,
        lastName: userStub().lastName,
        password: userStub().password,
      };
      const user: UserEntity = userStub();
      const spiedBcryptHashMethod = jest
        .spyOn(bcrypt, 'hash')
        .mockImplementation(() => Promise.resolve(''));
      const spiedRepositoryCreateMethod = jest
        .spyOn(userRepository, 'create')
        .mockReturnValue(user);
      const spiedRepositorySaveMethod = jest
        .spyOn(userRepository, 'save')
        .mockResolvedValue(user);

      const createUserResult = await userService.createUser(createUserDto);

      expect(spiedBcryptHashMethod).toHaveBeenCalled();
      expect(spiedRepositoryCreateMethod).toHaveBeenCalled();
      expect(spiedRepositorySaveMethod).toHaveBeenCalledWith(user);
      expect(createUserResult).toEqual(user);
    });
  });
});

Error is appeared here:

const spiedBcryptHashMethod = jest
        .spyOn(bcrypt, 'hash')
        .mockImplementation(() => Promise.resolve(''));

My userService code:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import * as bcrypt from 'bcrypt';

import { UserRepository } from './user.repository';
import { CreateUserDto } from './dto/create-user.dto';
import { UserEntity } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(UserRepository) private userRepository: UserRepository,
  ) {}

  public async createUser(createUserDto: CreateUserDto): Promise<UserEntity> {
    return await this.userRepository.save(
      this.userRepository.create({
        ...createUserDto,
        password: await new Promise((resolve, reject) => {
          bcrypt.hash(createUserDto.password, 10, (err, encrypted) => {
            if (err) {
              reject(err);
            }

            resolve(encrypted);
          });
        }).then((onFilled: string) => onFilled),
      }),
    );
  }
}

CodePudding user response:

Okay, so there's a lot to say about your service code...

The immediate issue you're having the problem is because you're mocking bcrypt's hash method to return a promise, but using the method as it returns a callback. IF you want to keep using the callback mixed with promises approach, you'd need to do something like

jest.spyOn(bcrypt, 'hash').mockImplementation((pass, salt, cb) => cb(null, ''))

This will be essentially the same as the Promise.resolve('').

HOWEVER I do not suggest this. Bcrypt has built in promise support, so instead of wrapping the callback with your own promise, you can just do await bcrypt.hash(pass, salt) and you'll get the hashed password back, then your Promise.resolve('') will work as you intend it too. I don't see the need for the then((onFullfilled: string) => onFullfilled) either, but that would go away if you remove the custom promise anyways.

Generally, try to stick with a single asynchronous approach. All callbacks (not suggested anymore), all promises (better), or all async/await (pretty much the standard now).

  •  Tags:  
  • Related