Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Error initializeTransactionalContext() #89

Open
sgentile opened this issue Feb 25, 2021 · 3 comments
Open

Testing Error initializeTransactionalContext() #89

sgentile opened this issue Feb 25, 2021 · 3 comments

Comments

@sgentile
Copy link

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
Copy link
Author

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. ?

@Elithsar
Copy link

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 ?

@psteinroe
Copy link

I was able to mock it using

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants