user1990009
user1990009

Reputation: 965

Sequelize transactional tests

Is it possible to wrap each sequelize test in a transaction and rollback at the end of each test? Which would be the recommended way to implement this with sequelize?

Also would it work if the code under test is also opening a transaction?(nested transactions)

Thanks

Upvotes: 2

Views: 2702

Answers (1)

Josh Rickert
Josh Rickert

Reputation: 309

I don't believe Sequelize supports nested transactions.

It sounds like you're attempting to maintain isolation between tests so that changes in one test don't affect another. My team accomplishes this by populating the test database with fixture data in a beforeEach declaration and truncating affected tables in an afterEach declaration. The tests run fairly quickly using this method.

To keep the tests running efficiently, you can narrow the focus of your cleanup step and configure per describe block to only truncate the handful of tables affected by each block of tests. For example, if you are testing your User model, add an afterEach to that file that will only truncate the User table.

Upvotes: 2

Related Questions