swc-project/swc · error

failed to find `mocha` from path

Error message

failed to find `mocha` from path

What it means

Execution tests run their generated test file under mocha. The harness locates the runner with `find_executable("mocha")` (crates/testing/src/lib.rs:54), which searches PATH for `mocha`/`mocha.exe`/`mocha.cmd`/`mocha.bat` and, failing that, tries `pnpm bin`. The `expect("failed to find `mocha` from path")` panics when neither yields a mocha executable — i.e. mocha is simply not installed anywhere the harness can see.

Source

Thrown at crates/swc_ecma_transforms_testing/src/lib.rs:667

            return Ok(());
        }
    }

    let tmp_dir = tempdir_in(&root).expect("failed to create a temp directory");
    create_dir_all(&tmp_dir).unwrap();

    let path = tmp_dir.path().join(format!("{hash}.test.js"));

    let mut tmp = OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .open(&path)
        .expect("failed to create a temp file");
    write!(tmp, "{src}").expect("failed to write to temp file");
    tmp.flush().unwrap();

    let test_runner_path = find_executable("mocha").expect("failed to find `mocha` from path");

    let mut base_cmd = if cfg!(target_os = "windows") {
        let mut c = Command::new("cmd");
        c.arg("/C").arg(&test_runner_path);
        c
    } else {
        Command::new(&test_runner_path)
    };

    let output = base_cmd
        .arg(format!("{}", path.display()))
        .arg("--color")
        .current_dir(root)
        .output()
        .expect("failed to run mocha");

    println!(">>>>> {} <<<<<", Color::Red.paint("Stdout"));
    println!("{}", String::from_utf8_lossy(&output.stdout));

View on GitHub (pinned to 5176682b65)

Solutions

  1. Install mocha in the workspace and ensure node_modules/.bin is resolvable: in the swc repo run `pnpm install`; elsewhere `npm i -D mocha` or `npm i -g mocha`.
  2. Ensure the package manager's bin dir is on PATH for the test process (or that `pnpm` is on PATH so the fallback works).
  3. Verify with `mocha --version` (or `npx mocha --version`) from the same shell that runs `cargo test`.
  4. If node tooling is intentionally absent, disable execution tests with `EXEC=0 cargo test`.

Example fix

# before
$ cargo test -p swc_ecma_transforms_testing   # mocha missing -> panic

# after
$ npm i -D mocha   # or: pnpm install
$ cargo test -p swc_ecma_transforms_testing
Defensive patterns

Strategy: validation

Validate before calling

// Run once in test setup (or CI preflight) to fail with a clear message
// instead of a mid-test panic.
fn require_mocha() {
    let found = ["mocha", "mocha.cmd", "mocha.bat"]
        .iter()
        .any(|name| which::which(name).is_ok());
    if !found {
        panic!("mocha not found: run `npm i -D mocha` (repo: `pnpm install`)");
    }
}

Prevention

When it happens

Trigger: Running any `test_exec!`-style execution test with `EXEC != 0` in an environment where mocha is absent: fresh clones without node_modules, global-only npm setups, PATH scrubbed by CI, and no `pnpm` on PATH to provide the fallback lookup.

Common situations: CI images with node but no mocha; running tests before `pnpm install` in the swc repo (mocha is a repo devDependency); environments using yarn/npm where the `pnpm bin` fallback cannot help.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/8f55ae2b43b4b6d5. Report an issue: GitHub.