jeudi 8 août 2019

How to mock TypeOrm Repository for ServiceTests in NestJS

I'm new to TypeOrm and have problems figuring out how to test my Service-Code. I have a Service where the Repository is injected with @InjectRepository. I want to test this service but I don't get the repository mocked.

I already tried some solutions provided in StackOverflow questions but none of them worked for me. Currently, I use 'useClass' with this class in my test:

export const mockRepository = jest.fn(() => ({
    metadata: {
        columns: [],
        relations: [],
    },
    find: jest.fn()
}));

But this gives me a TypeError "Cannot read property 'methodStubCollection' of undefined.". This is the test where the Error occures.

it('should return array with all Lists', async () => {
    const lists: List[] = [];
    when(repositoryMock.find()).thenReturn(lists);
    expect(await service.findAll()).toBe(lists);
});

Aucun commentaire:

Enregistrer un commentaire