Skip to content

Test API Reference

@korajs/test provides a testing harness for multi-device sync scenarios. It creates virtual networks with real SQLite stores and in-memory transports.

typescript
import {
  createTestNetwork,
  expectConverged,
  checkConvergence,
} from '@korajs/test'

import type {
  TestNetwork,
  TestDevice,
  TestServer,
  TestNetworkOptions,
  ConvergenceResult,
  CollectionDifference,
  FieldDifference,
} from '@korajs/test'

createTestNetwork()

Creates a virtual network with a server and multiple devices for testing sync behavior.

Signature

typescript
function createTestNetwork(
  schema: SchemaDefinition,
  options?: TestNetworkOptions
): Promise<TestNetwork>

Parameters

ParameterTypeDescription
schemaSchemaDefinitionThe schema for all devices and the server.
optionsTestNetworkOptionsOptional configuration.

TestNetworkOptions

FieldTypeDefaultDescription
devicesnumber2Number of devices to create.
deviceNamesstring[]['device-0', 'device-1', ...]Custom names for devices. Overrides devices count.

Returns

Promise<TestNetwork> -- A network object with server, devices, and cleanup.

TestNetwork

PropertyTypeDescription
serverTestServerThe virtual sync server.
devicesTestDevice[]Array of virtual devices.
tmpDirstringTemporary directory for SQLite databases.
close()Promise<void>Close all devices and the server, clean up temp files.

Example

typescript
const network = await createTestNetwork(schema, { devices: 3 })

try {
  // ... test logic
} finally {
  await network.close()
}

TestDevice

A virtual device with a real SQLite store, sync engine, and merge engine.

Properties

PropertyTypeDescription
namestringDevice name (for identification in logs).
storeStoreThe device's local store.
emitterKoraEventEmitterEvent emitter for instrumentation events.

Methods

.collection(name)

Returns a collection accessor for performing CRUD operations.

typescript
collection(name: string): CollectionAccessor
typescript
await device.collection('todos').insert({ title: 'Test' })

.sync()

Connects to the server and performs a full sync cycle.

typescript
sync(): Promise<void>

.disconnect()

Closes the current sync connection.

typescript
disconnect(): Promise<void>

.reconnect()

Re-establishes a sync connection to the server (disconnects first if connected).

typescript
reconnect(): Promise<void>

.getState(collectionName)

Returns all records in a collection as plain objects. Useful for assertions.

typescript
getState(collectionName: string): Promise<Record<string, unknown>[]>

.getNodeId()

Returns the device's unique node ID.

typescript
getNodeId(): string

.getVersionVector()

Returns the device's current version vector.

typescript
getVersionVector(): VersionVector

.isConnected()

Returns whether the device is currently connected to the server.

typescript
isConnected(): boolean

.close()

Releases all resources (closes store, disconnects sync).

typescript
close(): Promise<void>

TestServer

The virtual sync server that devices connect to.

Properties

PropertyTypeDescription
storeMemoryServerStoreThe server's in-memory operation store.

Methods

.getAllOperations()

Returns all operations stored on the server.

typescript
getAllOperations(): Operation[]

.getConnectionCount()

Returns the number of currently connected devices.

typescript
getConnectionCount(): number

.close()

Closes the server and all connections.

typescript
close(): Promise<void>

expectConverged()

Asserts that all devices have identical state across all collections. Throws a descriptive error if devices have not converged.

Signature

typescript
function expectConverged(
  devices: TestDevice[],
  schema: SchemaDefinition
): Promise<void>

Parameters

ParameterTypeDescription
devicesTestDevice[]Devices to compare.
schemaSchemaDefinitionSchema defining the collections to check.

Throws

Throws an error with message 'Devices have not converged' and detailed difference information if any device has different state.


checkConvergence()

Checks convergence without throwing. Returns a detailed result describing any differences.

Signature

typescript
function checkConvergence(
  devices: TestDevice[],
  schema: SchemaDefinition
): Promise<ConvergenceResult>

Returns

ConvergenceResult

FieldTypeDescription
convergedbooleantrue if all devices have identical state.
differencesCollectionDifference[]Details of any differences found.

CollectionDifference

FieldTypeDescription
collectionstringCollection name where difference was found.
deviceAstringName of the first device.
deviceBstringName of the second device.
missingInBstring[]Record IDs present in A but missing in B.
missingInAstring[]Record IDs present in B but missing in A.
fieldDifferencesFieldDifference[]Field-level differences for records present in both.

FieldDifference

FieldTypeDescription
recordIdstringThe record with differing values.
fieldstringThe field name that differs.
valueInAunknownValue on device A.
valueInBunknownValue on device B.

ChaosTransport

Re-exported from @korajs/sync. A transport wrapper that simulates unreliable network conditions for stress testing.

typescript
import { ChaosTransport } from '@korajs/test'

See the Sync API reference for ChaosTransport configuration options.