Crud: How to test a service that extends TypeOrmCrudService.

Created on 11 Aug 2019  路  7Comments  路  Source: nestjsx/crud

i am trying to unit test a basic service, which just extends typeormservice but gets the error below.
vlcsnap-2019-08-11-01h37m09s869
The service was implemented has follows
````typescript
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';
import { Authorization } from '../models/author.entity';
import { Repository } from 'typeorm';

/**

  • CRUD service for Authorization model
    */
    @Injectable()
    export class AuthorService extends TypeOrmCrudService {
    constructor(@InjectRepository(Authorization) public repository: Repository) {
    super(repository);
    }
    }
    ```
    Is there away to test or format, i had check through the typeormservice with little luck. thank you.

Most helpful comment

I resolved this kind of error by using the mock class presented by @Darkein here: https://github.com/nestjsx/crud/issues/325#issuecomment-566666542 and adding the metadata property like the following:
public metadata = { connection: { options: { type: null } }, columns: [], relations: [] }

So the final mock class for a repository is like below:

export class MockRepository<T> {
  public createQueryBuilder = jest.fn(() => this.queryBuilder);

  public manager = { transaction: a => Promise.resolve(a()) };
  public metadata = { connection: { options: { type: null } }, columns: [], relations: [] }

  public save = jest.fn();
  public delete = jest.fn();
  public update = jest.fn();
  public findOne = jest.fn();
  public findOneOrFail = jest.fn();
  public find = jest.fn();
  public getMany = jest.fn();

  public queryBuilder = {
    offset: jest.fn().mockReturnThis(),
    take: jest.fn().mockReturnThis(),
    orderBy: jest.fn().mockReturnThis(),
    skip: jest.fn().mockReturnThis(),
    limit: jest.fn().mockReturnThis(),
    from: jest.fn().mockReturnThis(),
    addFrom: jest.fn().mockReturnThis(),
    where: jest.fn().mockReturnThis(),
    andWhere: jest.fn().mockReturnThis(),
    innerJoinAndSelect: jest.fn().mockReturnThis(),
    leftJoinAndSelect: jest.fn().mockReturnThis(),
    getManyAndCount: jest.fn(),
    getMany: jest.fn(),
    getOne: jest.fn(),
    delete: jest.fn().mockReturnThis(),
    execute: jest.fn().mockReturnThis()
  };
}

All 7 comments

it turns out any mock repository must declare metadata for the tested service.
(https://github.com/nestjsx/crud/blob/b1f092477fc2fb8945858bf4e39726cd5544c53a/packages/crud-typeorm/src/typeorm-crud.service.ts#L326l). so i tested like these
test-code

Hi @avenuer , tried this. Is this still working for you?

It's still working @jakemonton for the base problem of columns, but i have extended it a bit more, a little of the snippets at https://gist.github.com/avenuer/e32320d3eac67e15cb478edd0c1f0c65

Not working for me:

TypeError: Cannot read property 'options' of undefined in constructor of service.

I resolved this kind of error by using the mock class presented by @Darkein here: https://github.com/nestjsx/crud/issues/325#issuecomment-566666542 and adding the metadata property like the following:
public metadata = { connection: { options: { type: null } }, columns: [], relations: [] }

So the final mock class for a repository is like below:

export class MockRepository<T> {
  public createQueryBuilder = jest.fn(() => this.queryBuilder);

  public manager = { transaction: a => Promise.resolve(a()) };
  public metadata = { connection: { options: { type: null } }, columns: [], relations: [] }

  public save = jest.fn();
  public delete = jest.fn();
  public update = jest.fn();
  public findOne = jest.fn();
  public findOneOrFail = jest.fn();
  public find = jest.fn();
  public getMany = jest.fn();

  public queryBuilder = {
    offset: jest.fn().mockReturnThis(),
    take: jest.fn().mockReturnThis(),
    orderBy: jest.fn().mockReturnThis(),
    skip: jest.fn().mockReturnThis(),
    limit: jest.fn().mockReturnThis(),
    from: jest.fn().mockReturnThis(),
    addFrom: jest.fn().mockReturnThis(),
    where: jest.fn().mockReturnThis(),
    andWhere: jest.fn().mockReturnThis(),
    innerJoinAndSelect: jest.fn().mockReturnThis(),
    leftJoinAndSelect: jest.fn().mockReturnThis(),
    getManyAndCount: jest.fn(),
    getMany: jest.fn(),
    getOne: jest.fn(),
    delete: jest.fn().mockReturnThis(),
    execute: jest.fn().mockReturnThis()
  };
}

@codeapostle you have test getOne, getMay, createOne, updateOne, replaceOne and deleteOne?

@mkolbusz Man, thank you so much! I was struggling with the - not explicit at all - can't read property "options" of undefined error. Works like a charm. 馃檹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbjhjm picture jbjhjm  路  4Comments

josimarz picture josimarz  路  4Comments

lafeuil picture lafeuil  路  5Comments

MeMeMax picture MeMeMax  路  4Comments

lalalalaluk picture lalalalaluk  路  4Comments