Juggernaut

Juggernaut logo: a dark armoured helmet with a glowing purple Elixir droplet on its brow

Fast mutation testing for Elixir, built for people and for coding agents.

Hex version Documentation on HexDocs Elixir 1.20 OTP 29 MIT licence

Quick start · Why Juggernaut · For coding agents · Dashboard · Docs

The Juggernaut dashboard: a gate run's verdict, mutant counts, scores, wall time and the score of each changed file against its threshold

Juggernaut changes your code in small ways (a > becomes >=, a true becomes false, a clause is dropped) and checks that your tests notice. Each change is a mutant; a mutant your tests fail on is killed, one they pass with has survived: a place where the tests do not pin the behaviour down. Coverage tells you a line ran; mutation testing tells you whether a test would notice if it were wrong.

Why Juggernaut

It is the mutation gate of three Elixir and Phoenix projects, and of Juggernaut itself (1,786 of 1,786 mutants killed). Every figure above comes from recorded runs in docs/benchmarks.md, which also records the caveats: single runs on a shared host vary by about 5%.

Quick start

mix igniter.install juggernaut   # or add the dependency by hand (below)
mix juggernaut                   # gate the functions changed against your base branch
mix juggernaut.explain ID        # one survivor in full, and why no test caught it

Write the test that tells the mutant from your code, then mix juggernaut.check ID to see it killed. For a faster loop, mix juggernaut --since HEAD~1 mutates only the clauses you changed, and mix juggernaut --watch runs the inner lane on every save.

Status: 0.4.0, pre-1.0. Elixir 1.20, OTP 29.

Install

With Igniter in the project:

mix igniter.install juggernaut

This adds {:juggernaut, "~> 0.4", only: [:dev, :test], runtime: false}, writes a .juggernaut.exs (Ecto projects get the Ecto query macros as raw_calls), and ignores /_build/juggernaut/ unless _build is ignored already. Running it again changes nothing. Options of mix juggernaut.install:

Without Igniter, add the dependency to mix.exs by hand:

def deps do
  [
    {:juggernaut, "~> 0.4", only: [:dev, :test], runtime: false}
  ]
end

Everything else works without Igniter: .juggernaut.exs is optional, and mix juggernaut.install --agents writes the agent guidance. The rest of the installer then says how to add Igniter and exits 2.

Upgrading from an earlier version, or using a read-only path dependency: see the upgrading guide.

Configure

.juggernaut.exs is optional; every key has a default. The common ones:

[
  paths: ["lib"],                                # what to mutate
  core: [MyApp.Billing, "lib/my_app/auth/**"],   # held to the core threshold
  thresholds: [core: 100, changed: 80, per: :file],
  base: "main",                                  # the git ref "changed" is measured against
  workers: :auto,                                # pool size
  timeouts: :killed,                             # :survived counts a kill only by timeout as a survivor
  no_coverage: :count,                           # :fail fails the gate on any mutant no test reaches
  raw_calls: [{Ecto.Query, :from}],              # calls whose arguments are never mutated
  env: [{"MIX_TEST_PARTITION", "${MIX_TEST_PARTITION}_jg{worker}"}],  # per worker
  cache: true
]

The file is validated: a wrong key or value names the key, its path and what was expected, and exits 2. Each worker gets its own MIX_TEST_PARTITION, so a Phoenix or Ecto project's test database is per worker; the steps of your test alias (such as ecto.create, ecto.migrate) run once per worker before its first job. A run refuses to start while credential-like variables (*_API_KEY, *TOKEN*, AWS_*, ...) are set, printing only their names (refuse_env). Every key, with its default, is described in the configuration guide.

To keep a mutant out on purpose, say why in a comment:

# juggernaut:ignore[string:empty] the empty suffix is covered by the parser tests
def suffix(text), do: text <> "!"

# juggernaut:ignore-start[collection] the order of words does not matter here
...
# juggernaut:ignore-end

mutare:ignore comments are read too. New ignores are listed in every summary.

Run

mix juggernaut                          # gate: functions in files changed vs base
mix juggernaut --full                   # every file under paths
mix juggernaut --files lib/my_app/billing.ex
mix juggernaut --since HEAD~3           # only the clauses changed since a ref
mix juggernaut --lane inner             # fast, advisory: changed clauses, fail fast, no control re-runs
mix juggernaut --lane release --full    # everything, nothing reused
mix juggernaut --dry-run --full         # list the mutants; run nothing

Lanes: inner for the edit loop (advisory, reads the cache), gate (the default: every operator, kills re-checked without the mutant, core survivors re-checked on a fresh worker, cache read and written on a clean committed tree), release (no reuse).

Other flags: --fail-fast, --max-mutants N and --budget 5m (what is left is reported as not run, never dropped silently), --workers N, --no-cache, --seed N, --page N and --per-page N, --output PATH, --strict-ignores, --no-server. mix help juggernaut lists them all; every task takes --help.

Output formats (--format, or JUGGERNAUT_FORMAT):

Format For
human the terminal: a progress line, each survivor as it is found (diff, tests that ran, the command that re-checks it), then the summary
agent one compact JSON document for coding agents (below)
json the mutation-testing-elements report
markdown pull requests and CI step summaries
github GitHub Actions annotations on the survivors' lines

--stream writes one JSON event per line as the run goes (with --format agent the last line is the agent document). Machine formats write only their document to stdout.

Exit codes

Code Meaning
0 the gate passed, or a read-only task succeeded
1 the gate failed, or the run is incomplete (mutants left unrun)
2 usage or configuration error: nothing was scored
3 infrastructure or harness error: never a verdict

Tasks

Task What it does
mix juggernaut run and gate (above)
mix juggernaut.check ID... run the named mutants again now, typically after adding a test; exit 0 when all are killed
mix juggernaut.explain ID one mutant in full: diff, source, status and kill kind, the killing test or the tests that passed, why they were chosen, the nearest miss
mix juggernaut.survivors a recorded run's survivors, grouped (--group-by file|module|operator), paged, --core-only
mix juggernaut.history one line per run, newest first, with trends of score, mutants, wall time and reuse
mix juggernaut.profile [RUN] where a run's time went: phases, job time by test selection, fallbacks, slow tests, workers, recycles, reuse
mix juggernaut.compare A B two runs mutant by mutant; --mutare REPORT.json compares with a Mutare or Stryker report
mix juggernaut.report writes the HTML dashboard (below)
mix juggernaut.triage ID --equivalent --reason TEXT records that a survivor is equivalent; --list, --revoke ID, --ack ID --as NAME
mix juggernaut.advise [RUN] which mutants need a fresh boot or the integration tests, test files that behave unlike their lane, the code seams that would make them cheaper; --verify runs boot-set mutants warm and fresh; --warm-safe, --ack, --revoke, --declarations manage warm-safe declarations
mix juggernaut.server the warm server (below); --status, --stop
mix juggernaut.install the Igniter installer (above)

A mutant id looks like Grant.check/2:relational:4be1c09. Any task accepts the whole id, its hash (4be1c09) or a unique prefix of four or more characters. --run takes a run id, latest (the default) or latest~N.

Triage claims live in .juggernaut.triage.json, which you commit. A claim lapses when the function it is about changes. A claim on core code needs a human's --ack; an acknowledgement from an actor whose name starts with agent is refused.

Code that runs while the app starts (the boot set) is judged on a fresh boot per mutant. mix juggernaut.advise says which mutants need one, and how to declare a function warm-safe: see boot set, seams and warm-safe declarations, which also covers tests that call ExUnit.run/0 themselves.

The warm server and --watch

mix juggernaut.server            # in one terminal; Ctrl-C, --stop or --idle-timeout ends it
mix juggernaut --files lib/a.ex  # elsewhere: attaches, same output and exit code
mix juggernaut --watch           # starts or attaches; the inner lane on every saved change

The server keeps one pool of workers between runs. The workers boot with the first run and afterwards only load the code a later run's compile changed, so a run attached to a warm server skips the pool boot (measured on the plain_lib fixture: 1.7 s cold, 2 ms warm). Requests queue first in, first out, and a client waiting behind another is told its position. The server holds the checkout's lock, so there is never a second pool: a --no-server run while it is up exits 3. It listens on 127.0.0.1 only, and every request carries the token in _build/juggernaut/server/token. Each request loads .juggernaut.exs again and checks the client's variable names against refuse_env; a change to .juggernaut.exs, mix.lock, mix.exs, anything under config/ or a test file gets a fresh pool, so an attached run always sees the same config as a standalone one.

For coding agents

Set JUGGERNAUT_FORMAT=agent (and JUGGERNAUT_ACTOR=agent:<name>, recorded with each run). The loop:

  1. mix juggernaut --lane inner after an edit (or keep mix juggernaut --watch open).
  2. Read summary first: verdict, exit_code, counts, failures (each rule that failed, with file and threshold), new_ignores, triage_claims.
  3. For each entry of survivors: diff, excerpt (the function's source), tests_ran (tests that ran with the mutant on and passed, with file:line), nearest_miss, suggested_test_file. Write the test that tells the two versions apart.
  4. Run the survivor's verify command (mix juggernaut.check <hash> --format agent): exit 0 means the new test kills it. With a server running it answers from the warm pool.
  5. If page.more is not 0, page.next is the command for the next page: nothing is cut silently. not_scored lists the mutants that could not be judged (compile or harness errors, mutants left unrun) with the reason.
  6. mix juggernaut.advise --format agent when runs are slow or survivors sit behind suite, file or boot selections: its seams and misclassified say what to change; next holds the commands an agent may run, for_a_person those it must not.

Exit 3 is never a verdict about your tests: report it rather than change tests for it. Do not add ignore comments or triage claims to make a gate pass without saying so; both are listed in every summary for the people who review it.

Every key of the agent documents, the error document and the --stream events is described in the agent output guide.

The dashboard

mix juggernaut.report writes _build/juggernaut/report/index.html (or --output): one file with its styles, script and data inline that makes no network requests, so it can be opened anywhere or attached to a CI build. Light and dark themes follow your system (or the toggle), and it works on a phone.

Each figure says whether it was measured by the run or recorded by an earlier one. Screenshots of every page and format, from real runs of the genserver_app fixture, are in docs/screenshots.

Data

Everything lives under _build/juggernaut/: history/runs.ndjson (one line per run: commit, dirty-tree hash, config fingerprint, actor, phase clocks, counts), runs/<id>/ (every mutant and result of a run), cache/ (reusable results), coverage/, sandbox/ (the mirror the mutants are compiled in; your working tree is never written) and server/. Runs and phases emit :telemetry events under [:juggernaut, ...] (Juggernaut.Telemetry).

Licence

MIT; see LICENSE. Code adapted from Mutare 0.5.1 keeps its notice in NOTICE.

Development

To work on Juggernaut itself, see CONTRIBUTING.md.