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

Unit Test Mocking doesn't work

Open icbrg opened this issue 1 year ago • 1 comments

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

icbrg avatar Feb 21 '24 11:02 icbrg

~~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:

xadamxk avatar Mar 19 '24 20:03 xadamxk