Wrap Mocha tests in transactions with mocha-slonik #336
AndrewJo
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey guys, I've made a small library that allows you to wrap tests in transactions which should help with making sure your tests are well isolated.
You can install it by running:
There are couple of modes of operation:
Registering a Mocha Root Hook plugin with
--require mocha-slonik/register
flag:mocha --require mocha-slonik/register tests/**/*.ts
This is the easiest way to get started if you're not utilizing factory design pattern for
createPool
or don't need advanced rollback strategy. Note that this option will globally modifycreatePool
to wrap all methods in a transaction and will break your tests if your code utilizescopyFromBinary
method.Example test.js:
Use the factory design pattern to manually wrap
createPool
method. This doesn't require modifying.mocharc
or adding--require
flags. This is a more advanced use case that will let you have greater control on when you can rollback your transactions (by letting you rollback inafter
/afterEach
block):Example test.js:
More details available on the mocha-slonik README.
Feel free to try it out and I hope it's useful for those who want to use transactions to isolate each of the test cases in your Mocha tests.
Beta Was this translation helpful? Give feedback.
All reactions