typeorm-transactional-cls-hooked
typeorm-transactional-cls-hooked copied to clipboard
Testing Error initializeTransactionalContext()
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 ?
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. ?
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 ?
I was able to mock it using
jest.mock('typeorm-transactional-cls-hooked', () => ({
Transactional: () => jest.fn(),
}));