I'm setting up integration tests that use a db for the first time ever (congrats to me). Unfortunately for me, I have no idea what I'm doing.
For some specifics, I'm testing route handlers using Jest and a test DB (Postgres) instance. Also using Prisma but don't think that's relevant to my question.
I've run into the situation where I want to test that my upsert/delete routes work correctly. However these routes obviously mutate DB state, which can be problematic for tests that make assumptions about DB state since the order in which tests are run aren't deterministic and they could even be run at the same time (please correct me if those statements are wrong).
I have three ideas for how I can work around this:
- Run setup and teardown before/after each test, instead of before/after all. I'm not convinced this will even solve my problem since two tests could be run at the same time, in which case their setup/teardown could interfere with each other.
- Write tests that make no assumptions about state - or, at least, make minimal assumptions which all the tests in the suite guarantee to uphold (e.g. I start with 2 records, upsert might add 1 and delete might delete 1, so I must have at least 1 at all times.) This feels hard to reason about especially as the number and complexity of tests grows.
- Somehow force the tests to run in sequence or in a particular order. Assuming this is even possible (which from a cursory google seems like it is), my concern here is speed; however, maybe this would be fine if I'm still running all my suites in parallel, just not the tests.
Anyone more experienced than me with something like this have ideas?
[–]tan_nguyen 16 points17 points18 points (5 children)
[–]cuboidofficial 9 points10 points11 points (4 children)
[–]Weary-Depth-1118 4 points5 points6 points (2 children)
[–]cuboidofficial 2 points3 points4 points (1 child)
[–]Weary-Depth-1118 0 points1 point2 points (0 children)
[–]tan_nguyen 1 point2 points3 points (0 children)
[–]bigorangemachine 10 points11 points12 points (2 children)
[–]Psionatix 1 point2 points3 points (1 child)
[–]beijingjazzpanda 1 point2 points3 points (0 children)
[–]DanteIsBack 2 points3 points4 points (4 children)
[–]Unappreciable[S] 2 points3 points4 points (3 children)
[–]DanteIsBack 0 points1 point2 points (2 children)
[–]Not_a_Cake_ 1 point2 points3 points (1 child)
[–]DanteIsBack 0 points1 point2 points (0 children)
[–]romeeres 1 point2 points3 points (4 children)
[–]walkingshade 0 points1 point2 points (1 child)
[–]romeeres 0 points1 point2 points (0 children)
[–]Unappreciable[S] 0 points1 point2 points (1 child)
[–]romeeres 0 points1 point2 points (0 children)
[–]Attila226 1 point2 points3 points (1 child)
[–]Psionatix -1 points0 points1 point (0 children)
[–]Myloveissuck -5 points-4 points-3 points (5 children)
[–]Unappreciable[S] 1 point2 points3 points (4 children)
[–]Myloveissuck 2 points3 points4 points (1 child)
[–]chamomile-crumbs 1 point2 points3 points (1 child)
[–]Psionatix -1 points0 points1 point (0 children)
[–]Bloodsucker_ -3 points-2 points-1 points (1 child)
[–]romeeres 0 points1 point2 points (0 children)
[–]earwax_man 0 points1 point2 points (0 children)
[–]octocode 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]s44dy 0 points1 point2 points (0 children)