Nest: Mock typeorm repository on Jest

Created on 12 Feb 2018  Â·  5Comments  Â·  Source: nestjs/nest

I am covering my Nest controller with some tests:

LabelsController.ts

import {Component} from '@nestjs/common';
import {InjectRepository} from '@nestjs/typeorm';
import {ILabelService} from './ILabelService';
import {Repository} from 'typeorm';
import {Label} from '../Models/Label';

@Component()
export class LabelsService implements ILabelService {

    private readonly labelRepository: Repository<Label>;

    constructor(@InjectRepository(Label)
                    labelRepository: Repository<Label>) {
        this.labelRepository = labelRepository;
    }

    async FindAll(): Promise<Label[]> {
        return await this.labelRepository.find();
    }

    async Find(code: string): Promise<Label> {
        return await this.labelRepository.findOne({Code: code});
    }

    async Where(label: Label): Promise<Label> {
        return await this.labelRepository.findOne(label);
    }

    async Insert(label: Label): Promise<Label> {
        await this.labelRepository.save(label);
        return label;
    }

    async Update(id: number, label: Label): Promise<Label> {

        try {
            await this.labelRepository.updateById(id, label);
            return label;
        } catch (e) {

        }
    }

    async Delete(id: number): Promise<Label> {
        try {
            const toDelete = this.labelRepository.findOneById(id);
            await this.labelRepository.deleteById(id);

            return toDelete;
        } catch (e) {

        }
    }
}

## LabelsController.test.ts
import { Test } from '@nestjs/testing';
import { LabelsController } from '../LabelsController';
import { LabelsService } from '../../Services/LabelsService';

describe('LabelsController', () => {
    let labelController: LabelsController;
    let labelService: LabelsService;

    beforeEach(async () => {
        const module = await Test.createTestingModule({

            controllers: [LabelsController],
            components: [LabelsService],
        }).compile();

        labelService = module.get<LabelsService>(LabelsService);
        labelController = module.get<LabelsController>(LabelsController);
    });

    describe('Get', () => {
        it('should return an array of labels', async () => {
            const result = ['test'];
            jest.spyOn(labelService, 'findAll').mockImplementation(() => result);

            expect(await labelController.root()).toBe(result);
        });
    });
});

Is there a way to mock/stub the Repository <Label> in my LabelsController.test.ts? Actually, I receive the following error on tests:
  LabelsController › findAll › should return an array of labels

    Nest can't resolve dependencies of the LabelsService (?). Please verify whether [0] argument is available in the current context.
type

All 5 comments

Hi @samueleresca
Please have a look at this https://github.com/nestjs/nest/issues/363#issuecomment-360105413

The default injection token for TypeORM repos is <entityName>Repository. You have to mock it and inject it consequently.

Duplicate

@samueleresca
Were you able to resolve this issue?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings