-
Notifications
You must be signed in to change notification settings - Fork 2
Description
So right now nearly all of our tests are in an "ideal" case, where the database is fresh with only a few entities in it (the ones relevant to the test).
This is an issue, since this means we're not testing if these endpoints/functions come with any side-effects from bad queries that would end up modifying data it shouldn't be. For example, if the endpoint for deleting a course ended up deleting users in the process through some poorly-set-up CASCADEs on entities.
My idea for solving this is as follows:
- in testUtils.ts, create a function that generates a TON of sample data. Multiple organizations, courses, users, events, questions (all 3 kinds), the lot of it. Ideally as close to how a "real" production database could look. The most maintainable way to do this is probably with factories rather than a manual query (it will be a lot slower, but we get type safety if the entities change in the future, which they will).
- in either a beforeAll or beforeEach statement for setupIntegrationTest, it runs this function to set up the dummy data
- in an afterEach or afterAll statement, it checks to make sure all dummy data is as it should be (probably via using a snapshot). Note that this might be somewhat tricky since most tests create entities of some kind, so you somehow want to exclude any of said data and only check the initial dummy data. You might need to come up with something custom for this depending on how things go, perhaps only querying for each specific piece of dummy data rather than querying to get the whole database and putting that in a snapshot.
- Initially, I would use beforeEach and afterEach. But I imagine setting up an entire DB full of dummy data is gonna significantly reduce how fast the tests run. So I'm thinking before you create a PR, probably change it to beforeAll and afterAll so it only runs once. The downside with this approach is if any test modifies the database in a way it shouldn't, it's not gonna be very granular to tell you which one. It's not gonna be an issue if all the tests pass, but if someone is adding a bunch of new tests they can probably just change it back to beforeEach or afterEach temporarily.
You could also see how other people go about doing this. There may even be something built into typeorm that I don't know about that integrates with jest that helps this where you can just pass some parameter with all the dummy data and it does the checking for us.
Note that there may be additional things that need to be adjusted. For example, I don't remember what line of code does the database reset between tests, but that one might need to be modified.