# mocha-js-reporter

> send JSON results of a mocha test to a callback function

Latest version **1.2.0** (published 2015-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install mocha-js-reporter
pnpm add mocha-js-reporter
yarn add mocha-js-reporter
bun add mocha-js-reporter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2015-08-18 |
| First published | 2015-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Dan Thareja |
| Maintainers | danthareja |
| Keywords | mocha, js, reporter |

## Links

- npm: https://www.npmjs.com/package/mocha-js-reporter
- Repository: https://github.com/danthareja/mocha-js-reporter
- Issues: https://github.com/danthareja/mocha-js-reporter/issues
- npm.io page: https://npm.io/package/mocha-js-reporter

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.2.0 (latest) — 2015-08-18
- 1.1.0 — 2015-08-14
- 1.0.1 — 2015-08-08
- 1.0.0 — 2015-08-08

## README

# Mocha JS Reporter
> send JSON results of a karma test to a callback function

## Usage
### Install the reporter
```
npm install mocha-js-reporter
```

### Run Mocha programmatically
```js
var fs = require('fs'),
    path = require('path'),
    Mocha = require('mocha'),
    MochaJSReporter = require('mocha-js-reporter')(Mocha);

// First, you need to define a function to handle your test results
var mochaCallback = function(testResults) {
  // Do anything you like with the test results here
  // ..
}

// Then, you need to instantiate a Mocha instance.
var mocha = new Mocha({
  // Returns a reporter that holds a callback in closure that
  // is executed on the results of the Mocha tests.
  reporter: new MochaJSReporter(mochaCallback)
});

// Then, you need to use the method "addFile" on the mocha
// object for each file.

// Here is an example:
fs.readdirSync('some/dir').filter(function(file){
    // Only keep the .js files
    return file.substr(-3) === '.js';

}).forEach(function(file){
    // Use the method "addFile" to add the file to mocha
    mocha.addFile(
        path.join('some/dir', file)
    );
});

// Now, you can run the tests.
mocha.run(function(failures){
  process.on('exit', function () {
    process.exit(failures);
  });
});
```
Note: `mochaCallback` will write JSON to `process.stdout` if not explicitly defined

## Example output
```js
{
  "stats": {
    "suites": 1,
    "tests": 2,
    "passes": 1,
    "pending": 0,
    "failures": 1,
    "start": "2015-08-08T17:15:28.232Z",
    "end": "2015-08-08T17:15:28.237Z",
    "duration": 5
  },
  "tests": [
    {
      "title": "should do something",
      "fullTitle": "orms-save-user-sequelize should do something",
      "duration": 2,
      "err": {},
      "code": "Object.keys(sequelize.models).length.should.equal(2);"
    },
    {
      "title": "should fail",
      "fullTitle": "orms-save-user-sequelize should fail",
      "duration": 1,
      "err": {
        "operator": "to be",
        "expected": 4,
        "message": "expected 3 to be 4",
        "showDiff": true,
        "actual": 3,
        "negate": false,
        "stack": "AssertionError: expected 3 to be 4\n    at Context.<anonymous> (sample-solution-code/problems/orms-save-user-sequelize/orms-save-user-sequelize.test.js:14:16)",
        "_message": "expected 3 to be 4",
        "generatedMessage": true
      },
      "code": "// something\n// some other comment\nvar myVar = 4;\n\n(3).should.equal(4);"
    }
  ],
  "pending": [],
  "failures": [
    {
      "title": "should fail",
      "fullTitle": "orms-save-user-sequelize should fail",
      "duration": 1,
      "err": {
        "operator": "to be",
        "expected": 4,
        "message": "expected 3 to be 4",
        "showDiff": true,
        "actual": 3,
        "negate": false,
        "stack": "AssertionError: expected 3 to be 4\n    at Context.<anonymous> (sample-solution-code/problems/orms-save-user-sequelize/orms-save-user-sequelize.test.js:14:16)",
        "_message": "expected 3 to be 4",
        "generatedMessage": true
      },
      "code": "// something\n// some other comment\nvar myVar = 4;\n\n(3).should.equal(4);"
    }
  ],
  "passes": [
    {
      "title": "should do something",
      "fullTitle": "orms-save-user-sequelize should do something",
      "duration": 2,
      "err": {},
      "code": "Object.keys(sequelize.models).length.should.equal(2);"
    }
  ]
}
```

---
_Source: https://npm.io/package/mocha-js-reporter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
