Aliheym / typeorm-transactional

A Transactional Method Decorator for TypeORM that uses Async Local Storage or cls-hooked to handle and propagate transactions between different repositories and service methods.
MIT License
198 stars 27 forks source link

Unit Test Mocking doesn't work #50

Open icbrg opened 6 months ago

icbrg commented 6 months ago

[x] NestJS [x] TypeORM

I have use jest to test function with Transactional decorator then I following these step to mock transaction for unit test mocking and get error from jest with message No storage driver defined in your app ... please call initializeTransactionalContext() before application start.

Do anyone have any solution to resolve this issue?

xadamxk commented 5 months ago

Running into the same issue @Aliheym

EDIT: Looks like someone already figured this out.

I updated

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

with

jest.mock("typeorm-transactional", () => ({
  Transactional: () => () => ({}),
  runOnTransactionCommit: () => () => ({}),
  runOnTransactionRollback: () => () => ({}),
  runOnTransactionComplete: () => () => ({}),
  initializeTransactionalContext: () => ({}), // seems to be required for testing
}));

And my tests are passing :heavy_check_mark: