Send Mocha Test Result to New Relic

Pavel Saman
3 min readJan 2, 2023

I’ve talked about Mocha test results a few times — in Mocha JSON Report(er) and Cache Mocha Test Results And Run Only Failed Test Files On Re-runs. Today I’ll add one more piece — sending Mocha test results to New Relic.

New Relic is a monitoring platform that can gather data from various sources. It also provides a couple of APIs you can use to upload almost any data (in a given format of course). New Relic gives you a free account you can use for max 100 GB of data ingest a month, which I find quite generous for testing purposes in order to get familiar with the platform.

It might be useful to see trends when it comes to automated checks. You know, how fast the checks run, what checks fail, etc. Typically if you run some checks in a pipeline, you will only see results for that given run, not trends over time. For that, you need to keep the data somewhere (at least for a period of time like 3 months). One option is New Relic.

What I did was I created a GitHub action that does exactly this — it sends Mocha test results to New Relic Log API. You can use it by adding one step in your job:

jobs:
test:
steps:
# ...
- name: Run tests
run: mocha --reporter json --reporter-option="output=results.json"
- uses: pavelsaman/send-mocha-test-results-to-nr@v1
with:
new-relic-license-key: ${{ secrets.NEWRELIC_LICENSE_KEY }}

There’re two prerequisites:

  • you need to use a correct NEWRELIC_LICENSE_KEYthat has permissions for data ingest (do this in New Relic in API keys setting)
  • Mocha has to generate a file with test results — that is done by running Mocha with --reporter-option="output=test-results.json" option; it can also be given to Mocha in a Mocha config file, e.g. .mocharc.json:
{
"reporter": "json",
"reporterOptions": [
"output=./results.json"
]
}

When set up, it will upload test results to New Relic. An example log data entry can look like this:

{
"git.branch": "master",
"git.ref": "refs/heads/master",
"git.sha": "63234d48bffbe4b49ff489c78e8b09c1735a6f23",
"github.action": "__pavelsaman_send-mocha-test-results-to-nr",
"github.actor": "pavelsaman",
"github.eventName": "workflow_dispatch",
"github.job": "test",
"github.project": "useful",
"github.runAttempt": 1,
"github.runId": 3824124059,
"github.runner.arch": "X64",
"github.runner.name": "GitHub Actions 5",
"github.runner.os": "Linux",
"github.runNumber": 4,
"github.workflow": "Test",
"logType": "test.case",
"message": "action-nr-test-results: test case PASSED",
"newrelic.logPattern": "action-nr-test-results: test case PASSED",
"newrelic.source": "api.logs",
"testDuration": 0,
"testFailure": false,
"testFile": "tests/max.test.js",
"testFullTitle": "max() undefined",
"testSuite": "max()",
"testTitle": "undefined",
"timestamp": 1672679710865
}

On the UI, it looks like this:

Example log entries in New Relic
Example log entries in New Relic

Once I have the test results in New Relic, I can build dashboard or query the logs. That can bring additional insight because I now have historic data as well.

Feel free to use it if you need it and find it useful. You can see the action in its repo here. There are a couple of inputs you can change to customize things a bit.

If you find my posts useful, please follow me to get notified about my posts in the future. Thank you :)

--

--