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:
.useFactory({
factory: () => drizzle(createClient({ url: ":memory:" }), { schema }),
})
.useFactory({
factory: () => drizzle(createClient({ url: ":memory:" }), { schema }),
})
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
Angelelz
Angelelz•11mo ago
Well, that factory key is a callback with its own closure Every time you call factory() its a new database
giovannibenussi
giovannibenussiOP•11mo ago
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 fails
Vinicius Gonçalves
Vinicius Gonçalves•11mo ago
Greetings! @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 🙂
Angelelz
Angelelz•11mo ago
Do you need to test the migrations or do you just need the database up and running?
Vinicius Gonçalves
Vinicius Gonçalves•11mo ago
I need it to be up and running! Running the migrations is just the prepatory step before executing the tests
Angelelz
Angelelz•11mo ago
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
Angelelz
Angelelz•11mo ago
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
Angelelz
Angelelz•11mo ago
For example, those are the better-sqlite3 tests
Vinicius Gonçalves
Vinicius Gonçalves•11mo ago
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 🙂
Neon
Neon•10mo ago
Hello @Vinicius Gonçalves , did you manage to do it ? I have the same problem
Vinicius Gonçalves
Vinicius Gonçalves•10mo ago
No. Unfortunately I didn't get an opportunity to work on it.
Want results from more Discord servers?
Add your server