【发布时间】:2021-03-04 21:20:43
【问题描述】:
我一直无法弄清楚如何在 react 本机应用程序中用 jest 模拟 SQLite 函数。被测代码如下所示:
//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;
};
到目前为止,我的测试代码如下所示:
//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());
});
我目前收到一个错误:SQLite.openDatabase.mockReturnValue 不是函数。
我正在使用 expo 39.0.2 和 expo-sqlite 8.4.0。
【问题讨论】:
标签: react-native sqlite jestjs expo