exunit-code-review
Reviews ExUnit test code for async safety, Ecto Sandbox usage, boundary mocking with Req.Test and Mox, and test adapters. Use when reviewing _test.exs files or test helper configurations.
SKILL.mdskills/exunit-code-review/SKILL.md
GitHubExUnit Code Review
Quick Reference
| Issue Type | Reference |
|---|---|
Async pitfalls, Application.put_env, CaptureLog, setup_all |
references/exunit-patterns.md |
Behavior-based mocking, $callers, Mox.allow |
references/mox-boundaries.md |
Req.Test, Bypass, Swoosh, Oban testing |
references/test-adapters.md |
| What to mock vs real, Ecto Sandbox, fixtures | references/integration-tests.md |
Mock Boundary Philosophy
Mock at external boundaries:
- HTTP clients, external APIs, third-party services (
Req.TestforReq,Bypass/Moxfor other clients) - Slow resources: file system, email (
Swoosh.Adapters.Test), job queues (Oban.Testing) - Non-deterministic:
DateTime.utc_now/0,:rand
DO NOT mock internal code:
- Contexts, schemas, GenServers
- Internal modules, PubSub
- Anything you wrote
Review Checklist
Test Structure & Async Safety
- Tests use
async: truewhen isolated (includinguse MyApp.DataCase, async: truevia Ecto Sandbox) -
async: falseused when mutating global state (Application.put_env/3, named singletons,{:shared, self()}sandbox, globalCaptureLog) -
setup_alldoes not leak sandbox connections or per-test state across tests
Mocking & HTTP Testing
-
Req.Test(stub/2,expect/3,json/2) used forReq-based HTTP clients (nimble_ownershipsupportsasync: truewithout ports) -
BypassorMoxused for non-Reqexternal boundaries (behaviors defined for interfaces) - No mocking of internal modules (contexts, schemas,
Repo) -
verify_on_exit!in setup for strictMoxmocking; noMox.allowrace conditions after process spawn
Test Adapters
-
Req.TestorBypassfor HTTP endpoint mocking -
Swoosh.Adapters.Testfor email testing -
Oban.Testingfor background job assertions (assertingOban.drain_queuereturn map%{success: _, failure: 0})
Database
-
Ecto.Adapters.SQL.Sandbox(start_owner!(Repo, shared: not tags[:async])) for isolation - Shared sandbox calls use
{:shared, self()}(not bare:shared) - Phoenix fixtures (
test/support/fixtures/) used consistently (ExMachinais legacy)
Valid Patterns (Do NOT Flag)
use MyApp.DataCase, async: true- Database tests run in isolated Ecto Sandbox transactions- Mock in unit test, real in integration - Different test levels have different needs
- Not mocking database in integration tests - Database is internal
- Simple inline test data - Not everything needs fixtures or factories
- Testing private functions via public API - Correct approach
Task.asyncinheritingMox/Req.Test/ Sandbox via$callers- Automatic in modern Elixir without manualallow
Context-Sensitive Rules
| Issue | Flag ONLY IF |
|---|---|
async: true race |
Test mutates global state (Application.put_env, named process, ETS, {:shared, self()}) |
| Missing mock | External call exists AND no Req.Test / Bypass / Mox |
| Mock internal | Module being mocked is internal code |
Gates (sequence)
Complete in order. Do not emit a finding until the prior step passes for that issue.
Evidence from the file — Open the test module (or helper) and tie the claim to concrete lines.
- Pass when: Each prospective finding includes
[FILE:LINE]and a one-line factual description of what is on that line (or an adjacent line you name), not a generic style complaint.
- Pass when: Each prospective finding includes
ExUnit false-positive veto — Check this skill’s Valid Patterns and Context-Sensitive Rules for the case.
- Pass when: You can state “not covered by Do NOT Flag / Flag ONLY IF” in one sentence, or you drop the finding.
Cross-protocol verification — Apply the
review-verification-protocolskill (review-verification-protocol) (e.g. read full function/block, search usages before “unused” claims) to that same finding.- Pass when: At least one protocol check relevant to the claim type is satisfied and would appear in your rationale if challenged.
Before Submitting Findings
Use [FILE:LINE] ISSUE_TITLE per finding after Gates (sequence) and the review-verification-protocol skill (review-verification-protocol) are satisfied.