Skip to main content
Fairyfly has no external test framework. Put files ending in .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 ff itself, 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 test run. There is no per-file grant.
  • The runner reads test/*.test.js as program files, so no --allow-read is needed just to load tests. Reads inside a test still need a scope.
  • test/run.sh runs each file with --allow-all because the suite covers filesystem, network, env, subprocesses, FFI, and the denial matrix itself. Tests that assert a denial must spawn a child ff with narrower flags instead of relying on the parent’s policy.
  • Minimal grants are better than --allow-all for application tests: a missing scope is a real failure you want to see.
Common grants: 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

Create test/smoke.test.js:
Run it:
This test touches no native API, so it needs no permission flags.

The check / done pattern

test/lib.mjs is tiny on purpose — read it once, then copy the pattern: Three rules:
  1. Every test file imports from ./lib.mjs — relative import with the .mjs extension.
  2. Every test file ends with done("<name>"). Without it the process may idle instead of exiting.
  3. done() calls process.exit(), which also stops the event loop. Server-fixture tests (which keep a listener alive) terminate cleanly because of this.
Real example from the repo (test/url.test.js):

Running tests: ff test

Behavior:
  • Opens test/ — a missing directory prints No 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.ffbc never runs under ff test).
  • Each file is evaluated as a module, then microtasks and the event loop are pumped — top-level await works.
  • Prints OK test/<name> or FAIL test/<name> per file, then a {passed} passed, {failed} failed summary.
  • Exit code is 1 when 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-all makes 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.
run.sh greps for the word FAIL, so any console.log("FAIL…") of your own marks the file failed. Only check() should ever print it.

Fuzz runner

Run zig 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-level await works, and the runner pumps the loop after each file:
For a server fixture, serve on a fixed test port, hit it, then done() (which exits and closes the listener):
Both grants are required: the listener binds 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:
Use the real global:
Note that 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 with PermissionDenied — 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