odavid / typeorm-transactional-cls-hooked

A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS
MIT License
524 stars 86 forks source link

Any way mocking @Transactional() #28

Closed sunjoong85 closed 5 years ago

sunjoong85 commented 5 years ago

Problem

I got this message in my test codes No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.

Question

Are there any ways to mock @Transactional() in my test codes?

My test codes are below.

describe('Approval', () => {
    test('Approve booking.', async () => {
      /**
       * Given
       * orderId
       */
      const orderId = '1234';

      const spy_OrderRepository_findOrder = jest.spyOn(orderRepository, 'findOrder');
      const spy_OrderRepository_saveOrder = jest.spyOn(orderRepository, 'saveOrder');

      /**
       * When
       * Host confirms booking request.
       */
     //confirmRequest is wrapped by @Transactional()
      await hostOrderService.confirmRequest(orderId');

     /**
     * Then
     *  Success
     */

Furthermore, is it possible to manage transactions in a service layer without this hook? Currently, NestJS integration is not possible to control typeorm transaction features among services, not repositories.

Great job! Thanks!

sunjoong85 commented 5 years ago

Oops.

I think calling ' initializeTransactionalContext()' works fine.

I will close this issue.

describe('Reservation Management', () => {
  let hostOrderService: HostOrderService;
  let orderRepository: OrderRepository;

  beforeAll(async () => {
    initializeTransactionalContext();

    const module = await Test.createTestingModule({
      providers: [HostOrderService, OrderRepositoryMock, PaymentServiceMock],
    }).compile();

    hostOrderService = module.get<HostOrderService>(HostOrderService);
    orderRepository = module.get<OrderRepository>(OrderRepository);
  });
sunjoong85 commented 5 years ago

Sorry. It didn't work.

Without DB connection I got this error.

 ConnectionNotFoundError: Connection "default" was not found.
sunjoong85 commented 5 years ago

I got it! Mock Transactional decorator