I have been having trouble figuring out how to mock SQLite functions with jest in a react native app. The code under test looks like this:
//helpers/db.js
import * as SQLite from 'expo-sqlite';
let db = null;
export const init = () => {
db = SQLite.openDatabase('places.db');
const promise = new Promise((resolve, reject) => {
db.transaction(tx => {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS places (id INTEGER PRIMARY KEY NOT NULL, title TEXT NOT NULL, imageUri TEXT NOT NULL, address TEXT NOT NULL, lat REAL NOT NULL, lng REAL NOT NULL);',
[],
() => {
resolve();
},
(_, err) => {
reject(err);
}
);
});
});
return promise;
};
So far, my test code looks like this:
//helpers.db.tests.js
import { init } from './db';
import * as SQLite from 'expo-sqlite';
test('loads exercises', async () => {
SQLite.openDatabase = jest.fn();
SQLite.openDatabase.mockReturnValue({ transaction: () => {} })
await init()
expect(SQLite.openDatabase).toBeCalledWith(expect.anything());
});
And I am currently getting an error: SQLite.openDatabase.mockReturnValue is not a function.
I am using expo 39.0.2 and expo-sqlite 8.4.0.