Testing in Jennifer

Jennifer

From the first test function to fake servers, fixtures and a CI gate.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 18

A test is a function whose name starts with test

use testing;

func testMath() {
    testing.assertEqual(2 + 3, 5);
    testing.assertTrue(len("abc") == 3);
}
$ jennifer test math_test.j
PASS testMath (0 ms)

1 passed, 0 failed, 1 total

Exit code 0 all passed, 1 a test failed, 2 the runner could not run.

2 / 18

The assertions, all seven

CallFails when
assertEqual(actual, expected)the values differ (deep: lists, maps, structs)
assertNotEqual(a, b)they are equal
assertTrue(cond), assertFalse(cond)the bool is the other one
assertContains(haystack, needle)substring, list element or map key is absent
assertThrows(name, kind)the function does not throw that kind
assertThrowsWith(name, args, kind)the same, with arguments
3 / 18

The overlay: cart_test.j beside cart.j

use testing;

func testParseQty() {
    testing.assertEqual(parseQty("3"), 3);
}

func testTotal() {
    def lines as list of Line init [
        Line{sku: "tea", qty: 2, cents: 350},
        Line{sku: "mug", qty: 1, cents: 900}
    ];
    testing.assertEqual(total($lines), 1600);
}
4 / 18

The module still declares its own use

$ jennifer test stock_test.j
stock.j:8:30: namespace `convert` is referenced but never declared (add
  `use convert;` or the matching `import`); the module under test must
  declare it itself (the test file's `use` / `import` does not reach a
  real import of the module) [L112]
use json;                    # stock.j forgot: use convert;
import "flatdb.j" as flatdb;
...
    def total as string init convert.toString($have + $n);
5 / 18

Errors: assertThrows takes a name

func badQty() {
    parseQty("abc");
}

func testParseQtyRejects() {
    testing.assertThrows("badQty", "cart");
    testing.assertThrowsArgs("parseQty", ["0"], "cart");
    testing.assertThrowsArgs("parseQty", ["100"], "cart");
}
[runtime] fails_test.j:27:5 testing.assertThrows: name must be string, got func
[runtime] fails_test.j:31:5 testing.assertThrows: function "soldOtu" is not defined
6 / 18

A wrong kind is a failure too

[assertion] fails_test.j:19:5 assertThrows: "notANumber" threw kind
  "runtime", expected "cart"
[assertion] stock_test.j:10:5 assertThrows: "soldOut" threw kind
  "stock", expected "rest"
try { price($api, "nope"); } catch (e) { $status = $e.status; }
testing.assertEqual($status, 404);
7 / 18

Table-driven tests: a loop

def struct Case { text as string, want as int };

func testParseQtyTable() {
    def cases as list of Case init [
        Case{text: "1", want: 1}, Case{text: "99", want: 99},
        Case{text: "007", want: 70}];
    for (def c in $cases) {
        def got as int init cart.parseQty($c.text);
        testing.assertEqual({$c.text: $got}, {$c.text: $c.want});
    }
}
[assertion] table_test.j:12:9 assertEqual: {"007": 7} != {"007": 70}
8 / 18

Order and shared state

use testing;
def seen as list of string init [];

func testB() { $seen[] = "b"; testing.assertEqual(len($seen), 1); }
func testA() { $seen[] = "a"; testing.assertEqual(len($seen), 1); }
$ jennifer test state_test.j
PASS testA (0 ms)
FAIL testB (0 ms)
     [assertion] state_test.j:4:31 assertEqual: 2 != 1
9 / 18

setUp, tearDown, --isolated

func setUp() {          # before every test
    $seen = [];
}

func tearDown() {       # after every test
    ...
}
$ jennifer test --isolated state_test.j
PASS testA (3 ms)
PASS testB (3 ms)
10 / 18

A throwaway store, cleaned up by defer

func testReceiveAddsUp() {
    def dir as string init fs.makeTempDir("", "stock-");
    defer fs.removeAll($dir);
    def path as string init $dir + "/stock.json";
    testing.assertEqual(receive($path, "tea", 5), 5);
    testing.assertEqual(receive($path, "tea", 3), 8);
}
11 / 18

A fake server on a loopback port

func testPrice() {
    def srv as httpd.Server init httpd.listen("127.0.0.1:0");
    defer httpd.shutdown($srv);
    def t as task of string init spawn {
        def req as httpd.Request init httpd.accept($srv);
        httpd.respond($req, 200, '{"cents": 350}');
        return httpd.path($req);
    };
    def base as string init "http://" + httpd.address($srv);
    def api as rest.Client init rest.client($base);
    testing.assertEqual(price($api, "tea"), 350);
    testing.assertEqual(task.wait($t), "/prices/tea");
}
12 / 18

Fixtures travel with the code

cp -R src /tmp/x && (cd /tmp/x && jennifer test shop_test.j)
13 / 18

Pick, format, measure

$ jennifer test --filter=Total --coverage cart_test.j
PASS testTotal (0 ms)

1 passed, 0 failed, 1 total

Coverage (statements):
  cart.j  4/11  (36.4%)
    uncovered: 10:5, 11:5, 12:9, 14:9, 16:5, 17:9, 19:5

--format=tap or --format=junit for CI tools. A filter that matches nothing exits 2.

14 / 18

Prove the test catches the bug

cp cart.j /tmp/cart.fixed.j
git show HEAD:cart.j > cart.j       # the code before the fix
jennifer test cart_test.j           # must fail
cp /tmp/cart.fixed.j cart.j         # the fix again
jennifer test cart_test.j           # must pass
FAIL testParseQty (0 ms)
     [assertion] cart_test.j:11:5 assertThrowsWith: "parseQty" did not throw
15 / 18

fmt and lint in CI

jennifer fmt -l src/*.j        # lists unformatted files, exit 1
jennifer lint src/*.j          # a warning fails, exit 1
for t in src/*_test.j; do jennifer test "$t" || exit 1; done
cart.j
cart_test.j
fails_test.j:27:26: warning: argument 1 to `testing.assertThrows`
  must be string, got func [L110]
16 / 18

Checklist

17 / 18

Read more

Based on Jennifer 0.27.0, November 2026.

18 / 18