-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add support for testing transaction commands #34
Comments
Hi, can you share a knex queries so I'll be able to understand the issue properly. |
The query itself is just the transaction command, which is already handled here: knex-mock-client/src/Tracker.ts Lines 44 to 49 in 5efcfe3
The problem is that I need to test that those command are being issued when they should, for example, testing that a callback is called strictly after the Since currently they are not being recorded in the history and also don't check any of the matchers, there is no way to test this. |
So, basically you would like to check which queries were executed in transaction, and if it was rollbacked or no? |
Yes, exactly. |
Another idea would be to generate a transaction ID and add it to the knex.transaction(async tx => {
await tx.insert(...);
// This query happens in the correct moment, after the transaction starts and before the transaction ends,
// but it happens outside of the transaction. There is no way to test this as well.
await knex.select(...);
}); I'll try to think of a way to catch these bugs without requiring a breaking change on |
This is exactly what I've thought 🙂 |
I'll draft a PR for these as a proposal |
I opened #35 with one solution for this that doesn't break any of the existing public APIs. There are some changes to the internal APIs though. Let me know what you think about it :) |
When implementing utilities to enter and exit transactions using knex, there is a need to test the ordering of transactional commands and how errors are handled.
The use case that got me here was implementing a contextual scope for cross repository transaction.
It is used like this:
SomeHandler
doesn't know anything about knex, and the repository interfaces only contain domain types. There is no API difference between an in-memory repository, a prisma repository or a knex repository.But the entire operation of
SomeHandler
should be done in a single transaction, ifBarRepository
fails to write, the changes to theFooRepository
must not be persisted.This is relatively simple to implement, but testing it requires checking if the correct
BEGIN
,SAVEPOINT xxx
,ROLBACK TO SAVEPOINT
andROLLBACK
commands are sent and in the correct order.Would you be willing to accept a PR to allow those commands to be checked on the
tracker.on.any
matcher, falling back to the current behavior of returning undefined when there is no match?I'd add them to the history if they ware matched, and keep the current behavior of dropping them otherwise, this way I don't think there would be any breaking change for existing users.
The text was updated successfully, but these errors were encountered: