首页 > 解决方案 > 如何在 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
})

标签: react-nativesqlitejestjsexpo

解决方案


推荐阅读