Litestone guide / Misc
Testing
@frontierjs/litestone/testing builds a throwaway database, a client and a factory per model in one call, so a test arranges rows without writing them by hand.
model Account {
id Int
name String
users User[]
}
model User {
id Int
email String
accountId Int
account Account (fields: [accountId], references: [id])
}
import { readFileSync } from 'node:fs'
import { makeTestClient } from '@frontierjs/litestone/testing'
const { db, factories } = await makeTestClient(readFileSync('./schema.lite', 'utf8'), { seed: 42, autoFactories: true })
await factories.user.withParents().createMany(3)
if (await db.user.count() !== 3 || await db.account.count() !== 1) throw new Error('expected 3 users under one parent account')
withParents() creates the account the required foreign key points at, so a child is one call. The database is a tmpdir file; nothing in the project is touched.
What else it can do
createTestEnvadds a principal (actingAs), a gate grid (atLevel) and migrates from committed files withmigrations:verifyGateLadder,verifyReadLadder,verifyFieldProtection,verifyConstraintsandverifyRowPoliciescheck the declared rules against real writesdefineFactoryandFactorytake states (users.admin().createOne()) and a seeded RNGhas(),attach()andfor()build children, many-to-many rows and one named parentSeederorders seed blocks withdependsOn, andSeeder.oncemakes one idempotentsnapshotandrestoreseed once and reset between tests;truncateandresetempty tablesenv.clockfreezes or moves the time a client readsgenerateGateMatrixandgenerateValidationCasesemit permission and boundary cases from the schemamutationScoremutates the schema and reports what the suite misses
In depth: Testing § createTestEnv, § makeTestClient, § Factory, § Relations, § Schema mutation testing