react-native - 如何在 Jest 中运行 expo-sqlite 测试?
问题描述
我有以下测试代码expo-sqlite
,
import { openDatabase } from "expo-sqlite"
const db = openDatabase("test.db")
jest.useFakeTimers()
function sleep(duration = 1000) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(duration)
}, duration)
})
}
describe("orm", () => {
test("throws error if two different types is used on same column", async () => {
db.transaction(tsx => {
tsx.executeSql(`SELECT * FROM USERS`)
})
await sleep(1000)
})
})
但是我收到以下错误,
TypeError: Cannot read property 'map' of undefined
at node_modules/expo-sqlite/src/SQLite.ts:27:41
我的 Jest 配置看起来像这样,
{
"jest": {
"preset": "jest-expo",
"setupFiles": [
"./jest-setup.js"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?@react-native|react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
jest-setup.js
像下面这样,
import "regenerator-runtime"
import "react-native-gesture-handler/jestSetup"
require("react-native-reanimated/lib/reanimated2/jestUtils").setUpTests()
jest.mock("react-native/Libraries/Utilities/Platform", () => {
const Platform = require.requireActual(
"react-native/Libraries/Utilities/Platform"
)
Platform.OS = "android"
return Platform
})
解决方案
推荐阅读
- c - 调用 dup/dup2 后关闭文件描述符的规则是什么?
- arrays - 垂直循环一个二维数组并从每一行中选择一个
- reactjs - 如何让选择器在 redux-form 应用程序中工作?
- python - 在python中的try-exception中打印帮助文本
- python - 如何设置纸浆约束的优先级
- mysql - 如何为未创建的对象创建新项目
- php - 从数据库中选择“变量”并在不刷新页面的情况下显示
- testing - 如何测试 Bit-Banged 通信的汇编程序
- spring - 带规范的可分页排序返回重复的结果
- google-cloud-firestore - 有没有办法使用 Firestore 安全规则使用 FieldValue.increment() 更新文档字段?