Better-sqlite3: Jest and better-sqlite3 problems with pragma

Created on 31 Oct 2018  路  2Comments  路  Source: JoshuaWise/better-sqlite3

Hi,
When using jest to mock better-sqlite3 in one file everything worked well but then when creating some sort of integration test (I checked that everything isn't mocked) I got to a weird problem that pragma throws "Expected first argument to be a string".

Stepping into the pragma code, I see that source is set to the expected value, and setPragmaMode.call(this,true) calls pragma function again with source is set to true

It is not consistent. When the other test file (with better-sqlite3 mocks) runs before the IT file, everything this error is thrown. When it's the opposite everything works just fine ( also when running only the IT it passes)

The regular code is working properly.

duplicate

Most helpful comment

For those with a similar issue, I managed to fix the problem by creating the pragmas via database.exec such as:

database.exec('PRAGMA journal_mode = WAL');

instead of:

database.pragma('journal_mode = WAL');

All 2 comments

Jest is not a well-behaved Node.js environment, and is known to have issues with native modules in general. I don't recommend using it for anything besides front-end code. This is a duplicate of https://github.com/JoshuaWise/better-sqlite3/issues/162. The issue is that Jest loads multiple copies of the libraries it imports/requires, but it's impossible to load multiple copies of a native C++ library within the same memory address space.

For those with a similar issue, I managed to fix the problem by creating the pragmas via database.exec such as:

database.exec('PRAGMA journal_mode = WAL');

instead of:

database.pragma('journal_mode = WAL');
Was this page helpful?
0 / 5 - 0 ratings