Files
test-samurai-mocha-runner/README.md

2.1 KiB

test-samurai-mocha-runner

Mocha.js runner for the test-samurai Neovim plugin.

Features

  • Detects Mocha test files by checking package.json dependencies.
  • Supports nearest, file, all, and failed-only commands.
  • Streams results via a custom NDJSON reporter.
  • Provides quickfix locations and per-test output.
  • Uses --grep with escaped patterns to match titles safely, even when running through npm test.
  • Executes tests via npx mocha with the bundled UI + reporter so projects need no extra setup.
  • TSamAll runs test/**/*.test.js by default.

Full Name Convention

The runner builds stable full names using the active suite stack joined by /, followed by the test title:

Suite/Subsuite/Test

This convention is used consistently in results.*, parse_test_output, and collect_failed_locations. Failed-only runs translate / back to spaces for Mocha's --grep matching. Avoid / in your titles if you rely on that mapping.

When the custom reporter provides titlePath, it is used to build full names without ambiguity (titles may contain spaces).

Reporter Payload

The runner uses the bundled NDJSON reporter at scripts/mocha-ndjson-reporter.cjs, which emits one JSON object per line:

{
  "event": "run-begin" | "run-end" | "suite" | "test",
  "status": "passed" | "failed" | "pending" | "skipped",
  "title": "Test or suite title",
  "fullTitle": "Mocha full title",
  "titlePath": ["Parent", "Child", "Test"],
  "file": "/path/to/test.js",
  "location": { "file": "/path/to/test.js", "line": 10, "column": 5 },
  "output": ["stdout line 1", "stdout line 2"],
  "error": { "message": "string", "stack": "string" }
}

The UI wrapper scripts/bdd-with-location.cjs attaches test/suite locations so Quickfix and per-test output are consistent.

Usage

Add the module to your test-samurai configuration:

require("test-samurai").setup({
  runner_modules = {
    "test-samurai-mocha-runner",
  },
})

Override the default glob for TSamAll:

require("test-samurai-mocha-runner").setup({
  all_test_glob = "test/**/*.test.js",
})

Development

Run tests:

bash run_test.sh