by holografix · 5y ago · 0 comments
I’m using express, mocha, supertest and typeorm.
Mocking the database is fucking black magic. So I gave up and simply use an in memory SQLite db and add some records in the test body for my API to fetch and return so I can test.
I’d like the db to basically get nuked and recreated after every test.
So I use “afterEach” to close the connection and create a new one.
No matter what I do, when I call the api endpoint using supertest it tells me my DB connection is closed.
You’re thinking maybe the previous test didn’t reopen it or something. But there only one test.
It’s like it’s closing it _before_ the test runs. As if afterEach is not waiting on the result from supertest (yes I awaited).
I spent a _whole day_ today trying to understand just what the fuck is going on.
Gimme goroutines _any day_.
Anecdata, but my experience with Ecto sandbox + thousands of tests says otherwise. Mocking would be faster? Probably. But IMO not worth the effort, given it's already fast enough.
To be fair, I have worked with Elixir codebases that generated humongous amount of data per-test, only to test a fraction of this data. This kind of test was slow, but it would be much more efficient to generate only the data you actually need.
Not only that, but if you're testing with the database, you get to test your queries. That seems worth it being a bit slower.
Tried it on-disk?