Assertions

For AI agents: this page is Markdown at https://squiggle.sh/docs/flows/assertions.md. Every docs page is listed at https://squiggle.sh/llms.txt, and all of them are one file at https://squiggle.sh/llms-full.txt.

An assert checks what the app did. Without one, a passing flow doesn’t prove that the app did the right thing. Add an assert after every step whose result matters.

Why a flow needs asserts

A tap passes when the app reacts in any way: something renders, the screen changes or a field takes focus. A tap on one of your app’s controls that gets no reaction to two touches fails with the tap did nothing (How Squiggle works lists the exceptions). Whether the reaction was the right one is up to an assert. Try the same flow against a broken Add to cart button, with and without an assert:

YAML
- tap: { testID: add-to-cart }
  1. taptestID=add-to-cart
  2. ✔ PASS 1 of 1 steps

The button showed its pressed state, so the app reacted and the step passes. Nothing checked that the cart changed.

Read a failureStudio tour · 5 steps

A tap that opens another screen can check that in the same step: with expectRoute, it fails unless the app reaches that screen within 5 seconds. See Name where a tap lands. An openLink takes the same expectRoute, so a link your app drops fails instead of passing on whatever rendered next.

What you can assert

YAML
- assert: { visible: { text: "1 item" } }- assert: { visible: { testID: cart-count, text: "3 items" } }- assert: { visible: { testID: skia-frames, textNumber: { gt: 0 } } }- assert: { route: OrderConfirmation }- assert: { rerenders: { CartRow: { lt: 3 } } }- assert: { droppedFrames: { lt: 2 } }- assert: { log: { field: event, equals: order.submitted } }- assert: { action: { checkout.submit: { eq: 1 } } }- assert: { store: { useCart.mutationCount: { gte: 1 } } }
AssertPasses when
visible · notVisible · existsA control is on screen, not on screen, or anywhere in the app. Takes a selector, which can also check what the control’s text says with textNumber or textMatches (see Selectors).
routeThe current screen has this name or path.
rerendersA component rendered fewer (or more) times during the step, such as { CartRow: { lt: 3 } }.
droppedFramesFewer frames were dropped during the step. The app measures its frame rate once a second, and the check counts only a measurement taken entirely after the step began, so it can wait up to 2.3 seconds for one. With none by then, it reports pending, not a pass or a fail.
storeA Zustand store exists, or changed a number of times: { store: { useCart.mutationCount: { gte: 1 } } }. The store is named by the variable it is declared as.
queryA TanStack Query has a status or was fetched a number of times.
logThe app logged a line: contains, regex, or a field of a logged event.
actionA named action ran a number of times. Needs defineAction in your app.
visualNothing overlaps, is clipped or is off screen (noOverlap, noClipping, onScreen), or the frame isn’t blank and text is readable (noBlankFrame, legibleContrast). To run the first three on every screen in light, dark and the largest text size, see squiggle flow --matrix.
memoryDeltaMemory grew less than a limit across the step before, such as a repeat in and out of a screen.
scrollAfter a scroll step: the list kept its frame rate, dropped few frames, and showed no tall blank band.
smoothAfter the step that starts a transition: the region never stalled, dropped few frames, and wasn’t driven from JavaScript.

Numbers take lt, lte, gt, gte, eq, or in with a list, such as { in: [1, 2] }. Any other operator, or an empty {}, stops the flow from loading. The Flow syntax reference has the full form of each.

Performance as an assert

A leak, a list that stutters and an animation that freezes are failed steps, named by the screen and the step they happened in:

YAML
# Going in and out of a screen four times gives its memory back.- repeat:    times: 4    commands:      - tap: { testID: open-checklist }      - back: {}- assert: { memoryDelta: { lt: 20MB } }# A list scrolls without stuttering or showing empty rows.- scroll: { direction: down, target: { testID: checklist-list } }- assert:    scroll:      fps: { gte: 40 }      droppedFrames: { lte: 20 }      blank: { lt: 60 }# A transition never freezes and isn't driven from JavaScript.- tap: { testID: open-details }- assert:    smooth:      region: { testID: details-card }      gapFrames: { lte: 8 }      droppedFrames: { lte: 12 }      jsDriven: false# A sheet slides up evenly: no stalls, judder or dropped frames.- tap: { testID: open-sheet }- assert:    smooth:      region: { testID: sheet }      droppedFrames: { lte: 12 }      jankFrames: { lte: 2 }      stallMs: { lte: 50 }      smoothness: { gte: 0.95 }
  • memoryDelta compares memory before and after the step right before it, so put it after a repeat that goes in and out of the screen. The limit is in bytes, or a size such as "1.5MB" (KB, MB and GB). It reads the app’s memory footprint, or its resident memory or JavaScript heap when that is all there is, and a failure names the screens the step entered. With fewer than two memory samples across the step, it reports pending: make the step longer, with more repeats.
  • scroll reads the list while the scroll step right before it runs. fps is the lowest frame rate sampled during the scroll. churn counts the components mounted and unmounted, which is high when a list throws rows away and rebuilds them. blank is the tallest empty band in the list, in points: rows that hadn’t rendered yet. It reads the list in list, a selector, or the scroll step’s target when you leave it out; with neither it reports pending. The assert fails when no list moved during the scroll, since a list that stood still shows no blank band.
  • smooth needs a region, and reads it every frame from the step right before it until the region holds still for a second, or for 6 seconds at most. The region follows its view while it moves, so it can be a view the step brings in. gapFrames is the longest stretch inside the motion where nothing in the region changed. jsDriven: false fails when something in the region re-rendered 20 or more times a second while it moved: that motion stops whenever JavaScript is busy.
  • jankFrames, stallMs and smoothness follow the region’s element, by its testID, at every frame the screen draws, for ms (default 2000, at most 10000; a longer window is refused) from the start of the step before. They follow the element’s own position on screen, so name the thing that moves (a sheet, a row), not a list whose content scrolls inside it. The element can be one the step brings in, such as a sheet a tap opens: it is followed from the first frame it appears in. Whether it moved at all is then read from the element, not the region. An element that stays put fails the check. A frame is jank when it belongs to a stall, a repeated or dropped frame, uneven steps, a jump or a change of direction. stallMs is the longest freeze while the element was moving, and smoothness is the share of moving frames with no jank, from 0 to 1. A failure names each problem with its time and frame numbers. These need Squiggle’s agent in the app, and report pending without it, for a region with no testID, or for an element that never appears within ms.

Take the numbers from your budgets

Leave a number out and scroll and smooth take it from the budget of the screen the assert runs on, so a limit lives in one place. A number you write in the flow wins. The "*" budget counts for every screen, and the one squiggle init writes sets fps ≥ 59 and droppedFrames ≤ 2, so an assert that leaves them out gets those limits. That is why the smooth examples above set their own droppedFrames.

AssertBudget it takes
scroll fpsframeP95Ms, as a floor: 1000 / frameP95Ms frames a second
scroll droppedFramesdroppedFramesPerInteraction
smooth droppedFramesdroppedFramesPerInteraction

scroll: {} takes both of its numbers from the budget. With no budget for that screen either, it reports pending. smooth with only a region checks that the region moved. The step’s result names the screen a number came from.

Check that every control takes its taps

--boundaries checks the edges of each labelled control on every screen a flow reaches. It hit-tests a point 1 pt inside and 1 pt outside each edge, and reports an edge where a tap inside lands on something else:

Terminal
squiggle flow settings --boundariesboundaries 2 screen(s) · 14 control(s) · 1 dead edge(s)  ✗ settings · save-button: the bottom edge: a tap at (196, 702), 1 pt inside it, reaches settings-footer    src/screens/Settings.tsx:88

A dead edge fails the run with exit code 1, like a failed step, and names the file and line of the control. The check needs Squiggle running inside the app, which a debug build on the simulator does by itself; without it, it says no screen could be swept.

Assert on events, not on wording

A log message gets reworded; a field doesn’t. Log a structured event where the thing happens in your app:

TypeScript
import { logEvent } from '@squiggle.sh/react-native';logEvent('order.submitted', { orderId, total, currency: 'EUR' });

In a React web app, import logEvent from @squiggle.sh/react; see React web apps.

Then check its fields in the flow:

YAML
- assert: { log: { field: event, equals: order.submitted } }- assert: { log: { field: currency, equals: EUR } }

This keeps passing when someone changes the message, and fails when the order stops being submitted. contains and regex work on any line your app already prints.

Squiggle reads what your app prints only when it launched the app with its output captured. On the simulator, run the flow with --fresh, or start it with - launchApp: { relaunch: always }, so it launches the app that way; an app you started from Xcode or with expo run:ios has nothing for these checks to read.

A goal for the whole flow

goal takes the same checks as assert, and is checked after the last step. The flow fails if the goal isn’t met, even when every step passed:

YAML
flow: checkoutgoal: { route: OrderConfirmation }steps:  - tap: { testID: checkout }  - tap: { text: Pay now }

Propose asserts from runs that passed

Squiggle can write asserts for what your screens have always shown. It reads the recordings of agent runs that passed, counts on how many visits to each screen a control with a testID was there, and proposes an exists assert for each one that was there on at least 90% of visits. Each assert goes into the shortest flow that reaches its screen, right after the step that confirms the flow got there, with the recordings it was seen in above it:

Terminal
squiggle contracts propose          # print the proposalsquiggle contracts propose --write  # add the asserts to your flowssquiggle contracts propose --pr     # and open a pull request with them
YAML
  - waitFor: { route: agent-lab, timeout: 4000 }  - assert: { route: agent-lab }  # contracts below were observed on a settled screen  - waitFor: { commitQuiet: true }  # contract: held on 54 of 54 green visits to agent-lab (intent-mvc2lphx-2-dann, …)  - assert: { exists: { testID: add-to-cart } }  - assert: { exists: { testID: checkout } }

Without --write or --pr, nothing changes. --pr commits only the flows it changed, on a new branch, and opens the pull request with the gh CLI. You merge it yourself. Run the changed flows before you merge: a control that was there on 9 visits in 10 can be missing on the tenth.

--min-confidence 1 proposes only what was there on every visit. A screen needs at least 3 recorded visits before anything is proposed for it. Squiggle leaves out controls your flow already checks, controls from the screen underneath, controls with only a label, and flows that are meant to fail. Screens no flow confirms it reached are counted at the end of the proposal, since there is no flow to put their asserts in.

When your app changes, the same command proposes removing the asserts that no longer hold. That covers an assert that made the last run of its flow fail, and one that newer recordings show on fewer than 90% of visits. Each removal names the run that shows it, so you can open that run before you agree. If an assert failed once and then passed on the next run, nothing is proposed. --write and --pr remove those asserts along with adding new ones. A removed assert isn’t proposed again until a recording newer than the failed run shows the control:

Terminal
squiggle flow open-agent-lab       # fails: exists testID=add-to-cartsquiggle contracts propose         # proposes removing add-to-cart, citing that runsquiggle contracts propose --write # removes it from the flow

Check your app’s navigation

Declare what should hold for every screen of your app, and Squiggle checks it over the screens and the moves between them it has recorded, without touching the app. You declare it per platform, like apps, and Squiggle finds your app’s recorded screens through apps.<platform> (or bundleId), so name the app there too. Name each screen by its full route, the one your agent’s status tool and the Screen map show: account/index for app/account/index.tsx, never index alone, which every stack has.

.squiggle/config.jsonc
"navigation": {  "ios": {    "home": "(tabs)/index",    "launch": "welcome",    "terminals": ["signed-out"],    "back": ["Back"],    "gates": { "account/index": ["sign-in"] },    "deepLinks": { "shop://order/42": "orders/[id]" }  }}
KeyWhat is checked
homeEvery screen leads back to this one. Required.
terminalsScreens allowed to have no way out. Every other screen must have one, and terminals don’t need to reach home.
backThe testID, label or text of your Back controls. Back from a screen lands on the screen it was opened from, and the edge swipe of a pushed screen counts as Back too. Left out, Back isn’t checked.
gatesA screen and its gates: no recorded path from launch reaches the screen without passing one of them.
deepLinksA link and the screen it opens: that screen is reachable from launch.
launchWhere gates and deep links are walked from. Defaults to home.
Terminal
squiggle contracts navigation              # every app declaredsquiggle contracts navigation --platform ios --json

Each check answers one of three ways:

  • proven: it holds on every screen Squiggle has recorded. The answer says how many screens and recorded moves (edges) that is.
  • falsified: a screen breaks it. The answer gives the shortest path that shows it, with the control to tap (or back) at each step. The command exits 1. A screen the latest tour finished without any control leading away is a dead end: the dead-end check fails on it, and so does the home check, unless it is under terminals. The tour never presses controls whose names say they crash, reset or sign out, so a screen whose only way out is one of those reads as a dead end too: run a flow that takes that way out, or list the screen under terminals.
  • inconclusive: Squiggle can’t say yet. The check reaches a screen nobody has left and no tour has finished, or a screen with no recorded Back when you set back; the answer lists those screens and the squiggle crawl --tour --only command that visits them. A screen meant to have no way out, such as a developer screen, goes under terminals. It’s also inconclusive when a screen you named was never recorded (check its spelling against the full routes status shows), when a short name fits several screens ([id] with both orders/[id] and users/[id]; the answer lists them), or when nothing is recorded yet. Tour the app and check again.

A screen named the short way (account for account/index, a last segment such as new, or a route without its group, tasks/index for (app)/tasks/index) is still checked as the one screen it fits, and the command prints a warning with the full route to write instead.

The system Back counts as a way out. When a tap pushes a screen, and every recorded push into that screen comes from the same one, swiping from the edge (or the browser’s Back on the web) returns there. Screens that switch with router.replace or as tabs don’t get one. If a screen turns off its back gesture, the check can still count one for it. Screens recorded by an earlier version of Squiggle don’t say which taps pushed them, so run squiggle crawl --tour once before you rely on the system Back.

A proof covers only what was recorded: a way out nobody has tapped isn’t known, so a falsified answer can be a missing tap rather than a bug. Try the path on the device before you fix anything (see below). Back is answered inconclusive on a screen opened from several places, because where Back goes there depends on how you got in. With no navigation key, the command says so and exits 0. Your agent gets the same answers from the invariants tool with action: "navigation", with the app connected.

Try a falsified answer on the device

--reproduce turns each falsified answer into a flow, saves it to .squiggle/flows/ as contract-<check>-<screen>, and runs it from a fresh launch: each step of the path (a tap, or Back), followed by a wait for the screen it should open. It runs on one device: a booted simulator, or the one --udid names (web:<origin> for a web app). Add --platform when your project declares more than one app.

Terminal
squiggle contracts navigation --reproduce --udid <udid>

For a screen that can’t reach home, it also saves and runs a second flow, contract-<check>-<screen>-back, that ends with the system Back and a wait for home. If that one gets home, your app has a way out Squiggle never recorded, and the answer says not reproduced: tour that screen, or tap its Back once, so the next check knows. The answer stays falsified, and the command still exits 1, until Squiggle records that way out. If the path itself no longer plays, a recorded move is out of date. Otherwise the answer says reproduced on the device, and in --json its counterexample has reproduced: true.