Code Monkey home page Code Monkey logo

Comments (7)

fwoelffel avatar fwoelffel commented on May 15, 2024 3

Here is what I came up with:

import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { AnnotationsService } from './annotations.service';
import { AnnotationEntity } from '../entities/annotation.entity';

describe('AnnotationsService', () => {
  let service: AnnotationsService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        AnnotationsService,
        {
          provide: getRepositoryToken(AnnotationEntity),
          useValue: {
            metadata: {
              columns: [],
              relations: [],
            }
          }
        }
      ],
    }).compile();

    service = module.get<AnnotationsService>(AnnotationsService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

This is far from perfect and I can't imagine keeping this kind of mock if I want to perform more complex tests. I think it is worth the pain to also ask this question to the TypeORM guys.

from crud.

fwoelffel avatar fwoelffel commented on May 15, 2024

Same issue here.
This is where the exception is thrown: https://github.com/nestjsx/crud/blob/master/src/typeorm/repository-service.class.ts#L31

from crud.

fwoelffel avatar fwoelffel commented on May 15, 2024

@zMotivat0r could you provide any instructions on how to test a RepositoryService?
Also, I'm wondering if the RepositoryService.{entityColumnsHash,entityRelationsHash} abstraction layer is really needed?

from crud.

michaelyali avatar michaelyali commented on May 15, 2024

So basically it's the same as you would mock TypeORM Repository in your project without using crud lib. I don't think that mocking it should be a part of this lib.

from crud.

fwoelffel avatar fwoelffel commented on May 15, 2024

Yes, I also think this issue is more related to TypeORM than to this library.

from crud.

fwoelffel avatar fwoelffel commented on May 15, 2024

There are apparently two main ways of doing things. See https://stackoverflow.com/questions/52797727/process-of-testing-with-typeorm-and-nestjs-and-jest-using-mocks for a good introduction.

from crud.

mathieugeissler avatar mathieugeissler commented on May 15, 2024

thanks it's works now with metadata field :)

from crud.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.