Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Mock Repository, Service and Controller In NestJS (Typeorm & Jest)

I'm new at typescript. My Nestjs project app is something like this. I'm trying to use repository pattern, so i separated business logic (service) and persistance logic (repository)

UserRepository

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { UserEntity } from './entities/user.entity';

@Injectable()
export class UserRepo {
  constructor(@InjectRepository(UserEntity) private readonly repo: Repository<UserEntity>) {}

  public find(): Promise<UserEntity[]> {
    return this.repo.find();
  }
}

UserService

import { Injectable } from '@nestjs/common';
import { UserRepo } from './user.repository';

@Injectable()
export class UserService {
  constructor(private readonly userRepo: UserRepo) {}

  public async get() {
   return this.userRepo.find();
  }
}

UserController

import { Controller, Get } from '@nestjs/common';

import { UserService } from './user.service';

@Controller('/users')
export class UserController {
  constructor(private readonly userService: UserService) {}

  // others method //

  @Get()
  public async getUsers() {
    try {
      const payload = this.userService.get();
      return this.Ok(payload);
    } catch (err) {
      return this.InternalServerError(err);
    }
  }
}

How do i create unit testing for repository, service & controller without actually persist or retrieve data to DB (using mock)?

like image 523
sundaem Avatar asked Mar 12 '20 10:03

sundaem


People also ask

Is it possible to mock a controller in nestjs?

Mocking in NestJS is pretty easily obtainable using the testing tools Nest exposes is @nestjs/testing. In short, you'll want to create a Custom Provider for the dependency you are looking to mock, and that's all there is. However, it's always better to see an example, so here is a possibility of a mock for the controller:

How to use Orm typeorm with nestjs?

Add these lines in the main module to say to nestJS that you will use the database configuration written in the .env file with the ORM Typeorm Add theses lines in the dogs module file to indicate that you will use the dog entity and the dog repository in the controller. Now let’s update your database with the changes you made.

How to connect to a nest database using TypeScript?

Since it's written in TypeScript, it works pretty well with the Nest framework. To start the adventure with this library we have to install all required dependencies: The first step we need to do is to establish the connection with our database using createConnection () function imported from the typeorm package.

What is the DTO for POST request in nestjs?

This DTO will define the format of the POST request you need to create the object. It is possible to test all these routes with Postman and see that it returns what is set in the controller. As you can see when testing your routes, nestJS uses the standard HTTP response codes for REST APIs (201 for POST, 200 for GET etc…)


1 Answers

Mocking in NestJS is pretty easily obtainable using the testing tools Nest exposes is @nestjs/testing. In short, you'll want to create a Custom Provider for the dependency you are looking to mock, and that's all there is. However, it's always better to see an example, so here is a possibility of a mock for the controller:

describe('UserController', () => {
  let controller: UserController;
  let service: UserService;
  beforeEach(async () => {
    const moduleRef = await Test.createTestingModule({
      controllers: [UserController],
      providers: [
        {
          provide: UserService,
          useValue: {
            get: jest.fn(() => mockUserEntity) // really it can be anything, but the closer to your actual logic the better
          }
        }
      ]
    }).compile();
    controller = moduleRef.get(UserController);
    service = moduleRef.get(UserService);
  });
});

And from there you can go on and write your tests. This is pretty much the same set up for all tests using Nest's DI system, the only thing to be aware of is things like @InjectRepository() and @InjectModel() (Mongoose and Sequilize decorators) where you'll need to use getRepositoryToken() or getModelToken() for the injection token. If you're looking for more exmaples take a look at this repository

like image 71
Jay McDoniel Avatar answered Oct 20 '22 19:10

Jay McDoniel