Skip to content

Test API Reference ​

@korajs/test builds in-process sync networks: one KoraSyncServer and several devices, each with a real SQLite store (better-sqlite3) and sync engine, connected by in-memory transports. Use it to test that your schema, resolvers, constraints and validators converge. The Testing guide shows patterns.

createTestNetwork(schema, options?) ​

typescript
const network = await createTestNetwork(schema, { devices: 3 })
try {
  const [a, b] = network.devices
  if (!a || !b) throw new Error('expected two devices')
  await a.sync()
  await b.sync()
  await a.collection('todos').insert({ title: 'from A' })
  await a.sync()
  await b.sync()
  await expectConverged(network.devices, schema)
} finally {
  await network.close()
}
OptionDefaultDescription
devices2Number of devices.
deviceNamesdevice-0, device-1, ...Names (overrides devices).
chaosnoneChaosConfig applied to every device link (dropRate, duplicateRate, reorderRate, maxLatency, randomSource, dropPredicate).
wrapTransportnoneWraps each device's transport pair (wrapTransportPairWithProtobufWire, wrapTransportPairWithServerClock(pair, now), createTransportMetrics().wrapTransport).
serverStorea MemoryServerStoreRun against SQLite or Postgres; the network closes it.
validateOperationnoneThe server's validator.
blobStoragefalseCentral blob storage on the server.
encryption / serverEncryptionnoneDevice encryption key material ({ config, salt, iterations? }) and the server's encryption policy.
deviceMaxOperationBytes256 KiBEach device store's maxOperationBytes.
legacyMergefalseDevices use the 1.0.0-beta.12 merge (comparison only).

TestNetwork: server, devices, tmpDir and close().

createMixedTestNetwork(serverSchema, serverOptions, deviceConfigs) creates devices on different schema versions ({ name, schema, syncSchemaVersion?, operationTransforms? }) to test migrations and operation transforms.

TestDevice ​

MemberDescription
name, store, emitterThe device's name, Store and event emitter (emitter.clear() resets recorded events).
collection(name)Collection accessor: insert, update, delete, findById, where.
sync()Connects (first call) and flushes pending operations.
disconnect() / reconnect() / isConnected()Connection control.
getState(collection)Every record as a plain object, for assertions.
getNodeId(), getVersionVector(), getSyncEngine(), getRejectedOperations()Introspection.
authChanged()Tells the engine its auth binding changed.
putBlob, stageBlob, pullBlob, pullBlobByRef, getBlobBytesBlob transfer.
close()Closes the store and sync.

Merge decisions are recorded in the device's audit trail as in an app.

TestServer ​

new TestServer(schema, options?) with store, schemaVersion, supportedSchemaVersions, operationTransforms, encryption, blobStorage, validateOperation. Members: store, ready, blobStore, getAllOperations(), getKoraContext() (the route context), authoritativeNodeIds, getConnectionCount(), retransmitPendingRelays(), getLiveBlobRefs(), close().

Convergence assertions ​

typescript
function expectConverged(devices: TestDevice[], schema: SchemaDefinition): Promise<void>
function expectConvergedEventually(
  devices: TestDevice[],
  schema: SchemaDefinition,
  options?: { timeoutMs?: number; intervalMs?: number },
): Promise<void>
function checkConvergence(devices: TestDevice[], schema: SchemaDefinition): Promise<ConvergenceResult>

interface ConvergenceResult { converged: boolean; differences: CollectionDifference[] }
interface CollectionDifference {
  collection: string
  deviceA: string
  deviceB: string
  missingInB: string[]     // record ids
  missingInA: string[]
  fieldDifferences: FieldDifference[]  // { recordId, field, valueInA, valueInB }
}

expectConverged throws with the differences when any pair of devices disagrees. expectConvergedEventually retries until the timeout, for networks with chaos.

typescript
const result = await checkConvergence([], schema)
result.converged // true: fewer than two devices always agree

Auth ​

createTestAuthBinding({ initialUserId?, anonymous? }) is a deterministic AuthSyncBinding that can move between loading, signed out and several users without an auth server, for testing per-user databases, held writes and reconnects.