Skip to content

A way to test transactional hooks? #34

@gaggle

Description

@gaggle

Hi,

I'm using typeorm-transactional-cls-hooked's hooks feature to emit events on transaction success. E.g. like this:

  @Transactional()
  async foo(): {
    const foo = await this.fooRepository.save();
    runOnTransactionCommit(async () => {
      event.emit()
    });
  }

But this library rejects all transactions by its very nature, so I can't test transaction-success logic. Uh oh.

Would it possible that this code here should manually fire off the success-hook? Something like:

    static async run(func: RunFunction) {
        await func();

+       // Fire all callbacks from typeorm-transactional-cls-hooked
+       getTransactionalContextHook.get('commit').forEach((cb)=>{cb()}
+       // I'm just pretending there's a way for this to work, I actually have no idea as I haven't made sense of the underlying code yet

        // Once the function has run, we throw an exception to ensure that the
        // transaction rolls back.
        throw new RollbackErrorException(
            `This is thrown to cause a rollback on the transaction.`,
        );
    }

WDYT? Am I asking for the impossible here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions