testjavascript / nodejs-integration-tests-best-practices

✅ Beyond the basics of Node.js testing. Including a super-comprehensive best practices list and an example app (March 2024)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🔥 Challenge: Migration tests recipe

goldbergyoni opened this issue · comments

Intergation tests are powerful becuase they allow testing the dark parts of the engine. Migrations are one of the darkest corners.

Two proposed tests:

  • Undo all migrations without failures - This proves that we can undo. But it doesn't promise anything about the logic
  • Ensure that migration is logically correct - Assuming that in v0.1 the field order.hasSupportTickets is optional and for some records it is null. Then in v0.2 this field is mandatory + New logic is presented: If this field is true, an order can not be deleted. The migration should set all NULL fields to true. A Typical test will insert records where this field is set (not null), it not even possible to insert an empty hasSupportTickets column because v0.2 doesn't allow this. But, in production for some rows it's null as they were inserted previously!

We can test this with:

test('When older Order record exist with empty hasSupportTickets and trying to delete it, then deletion fails with HTTP 409', () => {

// Arrange

migrate the DB to v0.1
insert an order with empty hasSupportTickets
migrate the DB to v0.2

// Act
try to delete the order

// Assert
...
}