Testing guide / Checks
The checks a schema contains
A gate, a row policy, a @guarded column and a @length are declarations, and a declaration can be executed. Four calls on the env run them against a real database, with no fixtures and no assertions to write, and each answers an array of mismatches, each with a message that is already a sentence.
model Account {
id Int
name String (1, 80)
token String?
(2.4.4.5)
}
import { createTestEnv } from '@frontierjs/testing'
const env = await createTestEnv({ schema: './schema.lite', autoFactories: true })
const found = [
...await env.verifyGateLadder(),
...await env.verifyFieldProtection(),
...await env.verifyConstraints(),
...await env.verifyRowPolicies(),
]
if (found.length) throw new Error(found.map((row) => row.message).join('\n'))
await env.close()
A passing suite is toEqual([]), and a failing one's message says what disagreed, such as Server.read at level 1 (VISITOR) — the schema says deny, the client says allow. They are four separate boundaries and a model can pass one while failing another, so a suite keeps them as four tests.
What else it can do
verifyGateLadder()checks every declared level of every gated model for read, create, update and deleteverifyReadLadder()checks reads aloneverifyFieldProtection()reads back every@guarded,@encryptedand@secretcolumnverifyConstraints()tries each validator with values on both sides of its boundary, and runs as system so a gate refusing first cannot make every case read as rejectedverifyRowPolicies()checks each@@allowand@@denyagainst rows on both sides of its predicateverifyTenantIsolation()checks that one tenant cannot see another's rows- A row with
got: "skipped"is a case the check could not grade, such as a create refused by a required@guardedfield before the gate was asked; itsmessagesays why - A throw that is not a refusal counts as a mismatch, never a pass, so an
@@externalmodel with no table fails rather than looks gated generateGateMatrixandgenerateValidationCasesemit the permission and boundary cases as data, for a test that wants to loop over them itselffli test:accesswritesdb/access.snapshot.md, the same ladder as a file a reviewer can read, andfli test:snapshotsrechecks every committed snapshotfli tutor:testis the lesson that builds an app and runs these four checks against it
In depth: Testing § createTestEnv, § verifyRowPolicies, test:access, tutor:test