question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test a service that extends TypeOrmCrudService.

See original GitHub issue

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

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<Authorization> {
  constructor(@InjectRepository(Authorization) public repository: Repository<Authorization>) {
    super(repository);
  }
}
 ``` 
Is there away to test or format, i had check through the typeormservice with little luck. thank you.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

30reactions
mkolbuszcommented, Mar 2, 2020

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()
  };
}
6reactions
KnyGoocommented, Feb 26, 2020

Not working for me:

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create a service test with NestJS + Nest Crud?
Does anyone know what is the correct way to test a service on NestJS using the Crud library? This is my service test...
Read more >
Testing Services | Nestjs-query - Blog
It is possible to test services that use TypeOrmQueryService. The process is similar to the one described for nestjs, but it has a...
Read more >
Developers - Extend CrudService as mixin - - Bountysource
... for creating a new TypeOrmCrudService class extensions and if it works I would like to try to also use this pattern for...
Read more >
NestJS — Fast & Simple CRUD RESTfull API - Medium
With @nestjsx/crud our service has only a few lines and it makes all ... export class UserService extends TypeOrmCrudService<UserEntity> {
Read more >
NestJS and React Admin with Amazon Lambda - Endertech
Swagger, which helps us visualize and test our API: ... { super(repo); } } @Injectable() export class UserTypesService extends TypeOrmCrudService<UserType> ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found