Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock typeorm repository on Jest #415

Closed
samueleresca opened this issue Feb 12, 2018 · 5 comments
Closed

Mock typeorm repository on Jest #415

samueleresca opened this issue Feb 12, 2018 · 5 comments

Comments

@samueleresca
Copy link

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.
@VinceOPS
Copy link

Hi @samueleresca
Please have a look at this #363 (comment)

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

@kamilmysliwiec
Copy link
Member

Duplicate

@KwabenBerko
Copy link

@samueleresca
Were you able to resolve this issue?

@ddehghan
Copy link

@lock
Copy link

lock bot commented Oct 16, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants