Error with NestJS + Turso in-memory database during testing
Hey everyone. First of all, I'm far from an expert with NestJS. However, I'm working on a test that mocks a database module as follows:
This works if you use
file:./test.db
instead of :memory:
. But when using the memory database it fails because there are no tables in the database.
What I know is that we run database migrations because it works when you use a file. However, for some reason the state is not persisted when using an in memory database.
Any ideas of what may be wrong in this case?11 Replies
Well, that factory key is a callback with its own closure
Every time you call factory() its a new database
Right. I added some logs and this function is only called once, which is expected but that was the only thing that makes sense as you mentioned
I even tried to extract the whole
drizzle(createClient({ url: ":memory:" }), { schema })
to a variable outside of all tests so I can be sure that it's the only instance and still failsGreetings!
@giovannibenussi brought the issue on my behalf when I thought it could be Turso's libsql fault.
I thought I could bring more info about the issue so we could understand what's going on:
- The factory method is called before each test of the test suite
- After the factory method being called, migrations are executed in said mock database using drizzle-kit
migrate
- The test fails due to: [LibsqlError: SQLITE_ERROR: no such table: users]
If you need more info, feel free to ping me 🙂Do you need to test the migrations or do you just need the database up and running?
I need it to be up and running!
Running the migrations is just the prepatory step before executing the tests
Without looking at how you are set up or what you're running, and assuming you're running integration tests, you might find it usefull to look at drizzle's own integration tests
For sqlite, you might want to take a look here first https://github.com/drizzle-team/drizzle-orm/blob/main/integration-tests/tests/better-sqlite.test.ts
GitHub
drizzle-orm/integration-tests/tests/better-sqlite.test.ts at main ·...
Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅 - drizzle-team/drizzle-orm
For example, those are the better-sqlite3 tests
I'm running unit tests in nestjs. Before each test I create a drizzle LibSqlDatabase and execute the migrations against it. The thing is, when using in-memory databases, it complains about inexistent tables. By Monday I'll bring a reproducible example so we can discuss 🙂
Hello @Vinicius Gonçalves , did you manage to do it ? I have the same problem
No. Unfortunately I didn't get an opportunity to work on it.