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
Uh oh!
There was an error while loading. Please reload this page.
-
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/registerflag:mocha --require mocha-slonik/register tests/**/*.ts{ "require": [ "mocha-slonik/register" ] }This is the easiest way to get started if you're not utilizing factory design pattern for
createPoolor don't need advanced rollback strategy. Note that this option will globally modifycreatePoolto wrap all methods in a transaction and will break your tests if your code utilizescopyFromBinarymethod.Example test.js:
Use the factory design pattern to manually wrap
createPoolmethod. This doesn't require modifying.mocharcor adding--requireflags. 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/afterEachblock):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