.test.js inside test/ and run them with ff test (one process, all files) or test/run.sh (one process per file).
Permissions
Test code runs under the same deny-by-default policy as any other script. Pass the grants the tests need to the runner:- Permission flags are parsed by
ffitself, not by the test filter. They may appear before or after the filter, and they are skipped when choosing which files to run. - A flag applies to every test file in that
ff testrun. There is no per-file grant. - The runner reads
test/*.test.jsas program files, so no--allow-readis needed just to load tests. Reads inside a test still need a scope. test/run.shruns each file with--allow-allbecause the suite covers filesystem, network, env, subprocesses, FFI, and the denial matrix itself. Tests that assert a denial must spawn a childffwith narrower flags instead of relying on the parent’s policy.- Minimal grants are better than
--allow-allfor application tests: a missing scope is a real failure you want to see.
For a server fixture that also fetches itself, grant both the listener and the
client URL:
0.0.0.0:3901 is the http.serve bind check; 127.0.0.1:3901 is the
outbound fetch check. Full model: Permissions.
Quick look
Createtest/smoke.test.js:
The check / done pattern
test/lib.mjs is tiny on purpose — read it once, then copy the pattern:
Three rules:
- Every test file imports from
./lib.mjs— relative import with the.mjsextension. - Every test file ends with
done("<name>"). Without it the process may idle instead of exiting. done()callsprocess.exit(), which also stops the event loop. Server-fixture tests (which keep a listener alive) terminate cleanly because of this.
test/url.test.js):
Running tests: ff test
- Opens
test/— a missing directory printsNo test/ directory found.and exits. Run from the project root. - Runs only files ending in
.test.js. Anything else is ignored (e.g.test/url.test.ffbcnever runs underff test). - Each file is evaluated as a module, then microtasks and the event loop are pumped — top-level
awaitworks. - Prints
OK test/<name>orFAIL test/<name>per file, then a{passed} passed, {failed} failedsummary. - Exit code is
1when any file fails. - Files larger than 10MB are not loaded (
FAIL … (read error)). - Permission flags in the command line apply to all files in the run;
--allow-allmakes every test file unrestricted.
Running tests: test/run.sh
Same job, one process per file:
run.sh passes --allow-all to every file — the suite covers fs, net, env,
run, FFI, and the denial matrix. The permission tests spawn child ff
processes with narrower flags, so the parent’s --allow-all does not hide a
missing check.
Green means every file exited 0 and printed no FAIL lines. make test
runs build + C fixtures, zig build test, then test/run.sh. make ci adds
zig fmt --check and a fuzz smoke run — the same gates as GitHub Actions.
Fuzz runner
Runzig build fuzz -Doptimize=ReleaseSafe -- <target|smoke> [seed] [--events-max=N]. Missing or unknown targets and invalid arguments print usage and exit with status 2.
Async and server tests
Top-levelawait works, and the runner pumps the loop after each file:
done() (which exits and closes the listener):
0.0.0.0:3901, and the client
fetches 127.0.0.1:3901.
Practical example: testing a JSON endpoint
Testing denials
To assert that a missing grant fails closed, run a child process with a narrower policy. The parent test runner can keep--allow-all; the child
starts clean:
execSync itself needs --allow-run=/bin/sh in the parent. The
repo’s test/permissions.test.js follows this pattern and builds the child
command lines with explicit scopes.
Troubleshooting
Tests fail withPermissionDenied — add the matching flag to the
ff test command. Common pairs: fetch needs --allow-net=<host>:<port>;
http.serve needs --allow-net=0.0.0.0:<port>; SQLite needs both file read
and directory write scopes.
A test passes under --allow-all but fails with narrow flags — the test
is missing a real grant. Add the specific scope rather than widening the
whole run. For server fixtures, remember the listener and client checks use
different addresses.
No test/ directory found. — run from the project root, not from inside test/.
Hanging test run — you forgot done(name), or a setInterval / listener keeps the loop alive with no exit path. End every file with done().
FAIL test/x.test.js (read error) — the file exceeds the 10MB per-file cap or is unreadable.
Passes under ff test, fails under run.sh — something printed FAIL without failing check(). Search the output for stray FAIL text.
Denial test passes unexpectedly under run.sh — the parent has
--allow-all. Spawn a child ff with explicit flags and assert its
PermissionDenied output; don’t rely on the parent’s policy.
See also
- Permissions — flags, scopes, denial contract
- Errors and exit codes — denial shapes
- child_process — spawning child
ffprocesses in tests - Limitations — every hard cap