Giter Club home page Giter Club logo

Comments (4)

B4nan avatar B4nan commented on May 31, 2024 9

Personally I rather use a real testing database and do integration tests, so not sure if I am the one to help you here :]

Btw all what EntityRepository does is to forward the calls to underlying EntityManager, so you could mock only the EM methods. (If you plan to use custom repositories, that is a different story for sure.)

from nestjs-realworld-example-app.

rhyek avatar rhyek commented on May 31, 2024 6

@B4nan , thanks. For a service method like this:

async create(values: CreateTodoDto): Promise<GetTodoDto> {
  const { name, description } = values;
  const todo = new Todo(name, description);
  await this.todoRepository.persist(todo);
  await this.em.flush();
  return todo as GetTodoDto;
}

My todo.service.spec.ts is initialized like so:

beforeEach(async () => {
  const module: TestingModule = await Test.createTestingModule({
    providers: [
      {
        provide: EntityManager,
        useFactory: jest.fn(() => ({
          flush: jest.fn(),
        })),
      },
      {
        provide: getRepositoryToken(Todo),
        useFactory: jest.fn(() => ({
          findAll: jest.fn(),
          findOne: jest.fn(),
          persist: jest.fn(),
        })),
      },
      TodoService,
    ],
  }).compile();

  todoService = module.get<TodoService>(TodoService);
  entityManagerMock = module.get(EntityManager);
  todoRepositoryMock = module.get(getRepositoryToken(Todo));
});

and then I just mock the repository methods' implementations in each unit test where applicable. It seems to be working fine. Do you think this approach is correct?

from nestjs-realworld-example-app.

B4nan avatar B4nan commented on May 31, 2024 2

Entity discovery needs to happen, if you want to work with entities (e.g. you want to work with the collection wrapper of some m:n or 1:m relations, or want to have propagation of changes working).

So yes, it is needed, as entity metadata are needed to work with entities. In v4, there is a way to only start the ORM (do the discovery), without connecting to database (there is connect: boolean param in MikroORM.init() - this will need additional support in the nestjs adapter, but that is not my project).

from nestjs-realworld-example-app.

rhyek avatar rhyek commented on May 31, 2024

alright. yeah i think i have the unit tests down so I'll go ahead and close this. thanks! :-)

from nestjs-realworld-example-app.

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.