typeorm-transactional-cls-hooked icon indicating copy to clipboard operation
typeorm-transactional-cls-hooked copied to clipboard

Testing Error initializeTransactionalContext()

Open sgentile opened this issue 3 years ago • 3 comments

So I'm using Jest and we are strictly doing unit tests -

I used the example

jest.mock('typeorm-transactional-cls-hooked', () => ({
            Transactional: () => () => ({}),
            BaseRepository: class {},
        }));

But it errors on initializeTransactionalContext(). Where would initializeTransactionalContext be used within a jest unit test ?

sgentile avatar Feb 25 '21 17:02 sgentile

When I added this it gives this message.

"ConnectionNotFoundError: Connection "default" was not found."

So is seems the mock is not handling mocking out the connection. ?

sgentile avatar Feb 25 '21 17:02 sgentile

Same problem here.

I can't figure out how to mock this @Transactional decorator.

Here is the used code.

let myService: MyService;
let myRepository: MyRepository;

  beforeEach(async () => {
    jest.mock('typeorm-transactional-cls-hooked', () => ({
      Transactional: () => () => ({}),
      BaseRepository: class {},
    }));
    initializeTransactionalContext();

    const module: TestingModule = await Test.createTestingModule({
      providers: [
        MyService,
        MyRepository,
        {
          provide: getRepositoryToken(MyRepository),
          useClass: MyRepositoryFake
        }
      ],
    }).compile();

    myService = module.get(MyService);
    myRepository = module.get(MyRepository);
  });

Am I doing something wrong ?

Elithsar avatar Apr 16 '21 14:04 Elithsar

I was able to mock it using

jest.mock('typeorm-transactional-cls-hooked', () => ({
  Transactional: () => jest.fn(),
}));

psteinroe avatar Jun 11 '21 12:06 psteinroe