mochajs/mocha · error · Error

ERR_MOCHA_UNSUPPORTED

ERR_MOCHA_UNSUPPORTED

Error message

file output not supported in browser

What it means

The XUnit reporter writes test results to a file given by reporterOptions.output. In browser bundles fs.createWriteStream does not exist, so the reporter throws ERR_MOCHA_UNSUPPORTED to signal file output is impossible. This prevents a confusing downstream crash when trying to write the stream.

Source

Thrown at lib/reporters/xunit.js:66

   * @param {Object} [options] - runner options
   */
  constructor(runner, options) {
    super(runner, options);

    var stats = this.stats;
    var tests = [];
    var self = this;

    // the name of the test suite, as it will appear in the resulting XML file
    var suiteName;

    // the default name of the test suite if none is provided
    var DEFAULT_SUITE_NAME = "Mocha Tests";

    if (options && options.reporterOptions) {
      if (options.reporterOptions.output) {
        if (!fs.createWriteStream) {
          throw createUnsupportedError("file output not supported in browser");
        }

        fs.mkdirSync(path.dirname(options.reporterOptions.output), {
          recursive: true,
        });
        self.fileStream = fs.createWriteStream(options.reporterOptions.output);
      }

      // get the suite name from the reporter options (if provided)
      suiteName = options.reporterOptions.suiteName;
    }

    // fall back to the default suite name
    suiteName = suiteName || DEFAULT_SUITE_NAME;

    runner.on(EVENT_TEST_PENDING, function (test) {
      tests.push(test);
    });

View on GitHub (pinned to 6bcbee4fd9)

Solutions

  1. Drop the output reporter option in browser environments; capture the reporter's XML string via runner events instead.
  2. Run xunit with file output only in Node via the mocha CLI.
  3. In browser tests, have the harness collect XUnit's output string and POST it to a server that writes the file.
  4. Use a separate Node-only mocharc (e.g. .mocharc.node.yml) for file-output reporting.

Example fix

// before
mocha.setup({reporter: 'xunit', reporterOptions: {output: 'out.xml'}});
// after
mocha.setup({reporter: 'xunit'}); // capture reporter output string in browser harness
Defensive patterns

Strategy: validation

Validate before calling

if (typeof window !== 'undefined' && reporterOptions && reporterOptions.output) {
  delete reporterOptions.output; // xunit file output requires Node fs
}

Type guard

function supportsXunitFileOutput() {
  return typeof fs !== 'undefined' && typeof fs.createWriteStream === 'function';
}

Try / catch

try {
  new Mocha({reporter: 'xunit', reporterOptions: {output: 'out.xml'}}).run(fn);
} catch (err) {
  if (err.code === 'ERR_MOCHA_UNSUPPORTED') {
    // switch to string-based xunit capture in browser
  }
}

Prevention

When it happens

Trigger: new XUnitReporter(runner, {reporterOptions: {output: 'xunit.xml'}}) executed in a browser (fs.createWriteStream undefined), e.g. via `mocha --reporter xunit --reporter-option output=report.xml` in a browser build.

Common situations: Browser-based CI runners reusing Node mocharc with xunit output; test frameworks embedding Mocha in the browser but requesting XML file reports; forgetting that xunit file output is Node-only.

Related errors


AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01). Data as JSON: /api/errors/958c86c434108884. Report an issue: GitHub.