Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run expo-sqlite tests in Jest?

I have following code for testing 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)
  })
})

However I get the following error,

TypeError: Cannot read property 'map' of undefined

  at node_modules/expo-sqlite/src/SQLite.ts:27:41

My Jest config looks like this,

{
  "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"
    ]
  }
}

And jest-setup.js like the following,

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
})
like image 599
Damon Avatar asked Dec 10 '25 18:12

Damon


1 Answers

I'm working on this recently.

If you need real query execution. You need to replace expo-sqlite with sqlite3 of nodejs because the Jest tests are run on your machine, not on the mobile device.

I'm trying to create wrapper of sqlite3 as expo-sqlite, but sqlite3 has a very different API - sqlite3 module doesn't have Promises API and do not provide same API style.


UPDATE

You can use this repository: https://github.com/zfben/expo-sqlite-mock

I confirmed the test was passed by following code

import {openDatabaseAsync, SQLiteDatabase} from "expo-sqlite";

describe(`Op sqlite not on Native Mobile Device`, () => {

  let db: SQLiteDatabase

  test(`The db client can be loaded`, async () => {
    db = await openDatabaseAsync(
      'NON_NATIVE_MOBILE_DEVICE',
    )

    expect(db).not.toBeNull()
  })

  test(`The db client can execute query`, async () => {
    const result = await db.getAllAsync("SELECT 1 AS COL1")
    expect(result).toHaveLength(1)
    expect(result[0]["COL1"]).toBe(1)
  })
})
$npm jest test

 PASS  components/__tests__/expo-sqlite-test.tsx
  Op sqlite not on Native Mobile Device
    ✓ The db client can be loaded (9 ms)
    ✓ The db client can execute query (3 ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.668 s, estimated 1 s
like image 189
user2930677 Avatar answered Dec 12 '25 17:12

user2930677



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!