NitroSQLite
Guides

Test SQLite in Node

Test shared SQLite connections, statements, and transactions in Node.

Both host packages export a Node mock. Use react-native-nitro-sqlite/mock for React Native or nitro-sqlite/mock for NitroUI. Neither entry point initializes the native runtime. See supported platforms for native host requirements. Install better-sqlite3 as a development dependency in your test project. The mock creates databases in a temporary directory and supports queries, atomic batches, prepared statements, and callback transactions. Separate connections share committed data and can keep consistent read snapshots under WAL.

Set up Jest

The examples below use the React Native package. For NitroUI, replace react-native-nitro-sqlite with nitro-sqlite in the mock mapping, imports, and /mock paths. Keep your test runner's TypeScript transform enabled for the NitroUI package's source entry points.

The package ships ES modules. Jest runs tests as CommonJS and skips transforming files in node_modules by default, so add react-native-nitro-sqlite to the packages your transformIgnorePatterns lets Jest transform. If your pattern already lists other packages, add it to that list:

// jest.config.js
module.exports = {
  // ...your existing configuration
  transformIgnorePatterns: [
    'node_modules/(?!((jest-)?react-native|@react-native(-community)?|react-native-nitro-sqlite)/)',
  ],
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
}

Then reset the mock databases in that setup file:

// jest.setup.ts
import { resetAllDatabases } from 'react-native-nitro-sqlite/mock'

jest.mock('react-native-nitro-sqlite', () =>
  require('react-native-nitro-sqlite/mock'),
)

afterEach(resetAllDatabases)

Tests can then import the package as they do in the app:

import { open } from 'react-native-nitro-sqlite'

test('stores a note', () => {
  const db = open({ name: 'notes.sqlite' })
  db.execute('CREATE TABLE notes (title TEXT NOT NULL)')
  db.execute('INSERT INTO notes (title) VALUES (?)', ['Draft'])

  expect(db.execute('SELECT title FROM notes').rows._array).toEqual([
    { title: 'Draft' },
  ])

  db.close()
})

The mock returns query rows through both results and rows, supports rows as arrays through executeRaw() and executeRawAsync(), and supports atomic batches. Column metadata is present for row-producing statements, including an empty SELECT or RETURNING result. It reports each column's declared type and position, and keeps the first declaration when column names repeat, as the native adapter does. Bound undefined becomes SQL NULL. A query that fails throws from execute() or rejects from executeAsync().

You can also import the mock directly instead of mapping the native entry point. For example, in a NitroUI project's Node test:

import { open, resetAllDatabases } from 'nitro-sqlite/mock'

afterEach(resetAllDatabases)

test('opens a temporary database', () => {
  const db = open({ name: 'notes.sqlite' })
  expect(db.execute('SELECT 1 AS value').rows.item(0)?.value).toBe(1)
  db.close()
})

Share a database between connections

Connections with the same name and location open the same temporary file. A default connection reserves its name until you close it. Set connection: 'independent' to open another handle, and readOnly: true to prevent writes. Read-only opens fail if the file does not exist.

const writer = open({ name: 'notes.sqlite' })
writer.execute('PRAGMA journal_mode = WAL')
writer.execute('CREATE TABLE notes (title TEXT)')
writer.execute('INSERT INTO notes VALUES (?)', ['Draft'])

const reader = open({
  name: 'notes.sqlite',
  connection: 'independent',
  readOnly: true,
})
expect(reader.execute('SELECT title FROM notes').rows.item(0)?.title).toBe(
  'Draft',
)

reader.close()
writer.close()

location selects a relative directory inside the mock's temporary directory. A name must be a file name; paths that escape the temporary directory throw.

Closing a connection preserves its data for reopening. delete() closes the connection and removes its database and sidecar files. It throws for a read-only connection or if another open connection uses the file. Close those connections before deleting it.

Reuse a prepared statement

Preparing SQL once lets you execute it repeatedly with new parameter values. Each execution replaces the previous bindings. Omitted values bind as SQL NULL. Finalize statements before closing the connection.

const db = open({ name: 'notes.sqlite' })
db.execute('CREATE TABLE notes (title TEXT)')
const insert = db.prepare('INSERT INTO notes VALUES (?)')

await insert.executeAsync(['Draft'])
await insert.executeAsync(['Published'])
insert.finalize()
expect(insert.isFinalized).toBe(true)

db.close()

finalize() is safe to repeat while the connection is open and idle. Executing a finalized statement or using a statement after its connection closes throws or rejects.

Commit or roll back a transaction

A transaction groups operations into one commit. The mock keeps a callback transaction exclusive on its connection, including across await. Use the supplied tx for all database work inside the callback:

const db = open({ name: 'notes.sqlite' })
db.execute('CREATE TABLE notes (title TEXT)')

const count = await db.transaction(async (tx) => {
  await tx.executeAsync('INSERT INTO notes VALUES (?)', ['Draft'])
  return tx.execute('SELECT title FROM notes').rows.length
})
expect(count).toBe(1)

db.close()

The callback's result resolves after commit. A callback error rolls back its changes. You can call tx.commit() or tx.rollback() explicitly; subsequent operations on that transaction fail. Await all tx.executeAsync() calls before a synchronous transaction operation or before returning from the callback.

Async connection calls run in call order and wait for callback transactions. Synchronous queries, preparation, statement finalization, close, and delete throw while their connection is busy. Awaiting a queued connection call inside its transaction callback deadlocks; use tx instead. Independent connections have separate queues.

Clean up between tests

Await all pending operations before calling resetAllDatabases(). It closes every handle and removes all temporary databases and sidecar files, including files whose connections you already closed. Connections and statements from before the reset cannot execute again.

The reset throws if any connection is busy. Await that work and retry. No connections close when this check fails.

API reference and limits

The mock ignores encryptionKey and writes plaintext databases. Test SEE encryption and native encryption errors on a device.

The generated mock API reference documents open(), NitroSQLite.open(), MockConnection, and resetAllDatabases(). Connection methods use the same parameter and result types as the native API.

The mock exports open, NitroSQLite.open, resetAllDatabases, and the MockConnection type. It does not replace every native package export. It does not implement attachments, SQL file imports, or direct NitroSQLite.native methods. Its SQL runs synchronously on Node's thread; async methods schedule that work and return promises. It uses the SQLite version bundled with better-sqlite3, which can differ from the library's native SQLite build. Use device tests for native integration, thread behavior, lock contention, and performance.