From 01f1cc2d8bbd5135d415ee70ad40e15e4fb78f6a Mon Sep 17 00:00:00 2001
From: James Sumners <jsumners@newrelic.com>
Date: Tue, 23 Jul 2024 10:19:38 -0400
Subject: [PATCH] address feedback

---
 package.json              |  6 +++---
 test/lib/test-reporter.js | 41 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 3 deletions(-)
 create mode 100644 test/lib/test-reporter.js

diff --git a/package.json b/package.json
index 597a3ecf70..6fcb397cc2 100644
--- a/package.json
+++ b/package.json
@@ -161,8 +161,8 @@
     "bench": "node ./bin/run-bench.js",
     "docker-env": "./bin/docker-env-vars.sh",
     "docs": "rm -rf ./out && jsdoc -c ./jsdoc-conf.jsonc --private -r .",
-    "integration": "npm run prepare-test && npm run sub-install && time c8 -o ./coverage/integration node --test test/integration/**/*.tap.js",
-    "integration:esm": "time c8 -o ./coverage/integration-esm node --test --loader=./esm-loader.mjs test/integration/**/*.tap.mjs",
+    "integration": "npm run prepare-test && npm run sub-install && time c8 -o ./coverage/integration node --test-reporter ./test/lib/test-reporter.js --test test/integration/*.tap.js test/integration/**/*.tap.js",
+    "integration:esm": "time c8 -o ./coverage/integration-esm node --loader=./esm-loader.mjs --test-reporter ./test/lib/test-reporter.js --test test/integration/**/*.tap.mjs",
     "prepare-test": "npm run ssl && npm run docker-env",
     "lint": "eslint ./*.{js,mjs} lib test bin examples",
     "lint:fix": "eslint --fix, ./*.{js,mjs} lib test bin examples",
@@ -175,7 +175,7 @@
     "sub-install": "node test/bin/install_sub_deps",
     "test": "npm run integration && npm run unit",
     "third-party-updates": "oss third-party manifest --includeOptDeps && oss third-party notices --includeOptDeps && git add THIRD_PARTY_NOTICES.md third_party_manifest.json",
-    "unit": "rm -f newrelic_agent.log && time c8 -o ./coverage/unit node --test test/unit/**/*.test.js",
+    "unit": "rm -f newrelic_agent.log && time c8 -o ./coverage/unit node --test-reporter ./test/lib/test-reporter.js --test test/unit/*.test.js test/unit/**/*.test.js",
     "unit:scripts": "time c8 -o ./coverage/scripts-unit node --test bin/test/*.test.js",
     "update-cross-agent-tests": "./bin/update-cats.sh",
     "versioned-tests": "./bin/run-versioned-tests.sh",
diff --git a/test/lib/test-reporter.js b/test/lib/test-reporter.js
new file mode 100644
index 0000000000..0568c3bf51
--- /dev/null
+++ b/test/lib/test-reporter.js
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2024 New Relic Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+// This file provides a custom test reporter for the native test runner
+// included in Node.js >=18. The default `spec` reporter writes too much
+// information to be usable in CI, and the `dot` reporter hides which tests
+// failed. This custom reporter outputs nothing for successful tests, and
+// outputs the failing test file when any failing test has occurred.
+//
+// See https://nodejs.org/api/test.html#custom-reporters.
+'use strict'
+
+const { Transform } = require('node:stream')
+const testReporter = new Transform({
+  writableObjectMode: true,
+  transform(event, encoding, callback) {
+    if (event.type !== 'test:fail') {
+      // We don't want to write out anything for any cases other than the
+      // failure case.
+      return callback(null, null)
+    }
+
+    // Once v18 has been dropped, we might want to revisit the output of
+    // failure cases. The `event` object is supposed to provide things like
+    // the failing line number and column, along with the failing test name.
+    // But on v18, we seem to only get `1` for both line and column, and the
+    // test name gets set to the `file`. So there isn't really any point in
+    // trying to provide more useful reports here while we need to support v18.
+    //
+    // The issue may also stem from the current test suites still being based
+    // on `tap`. Once we are able to migrate the actual test code to `node:test`
+    // we should revisit this reporter to determine if we can improve it.
+    //
+    // See https://nodejs.org/api/test.html#event-testfail.
+    callback(null, `failed: ${event.data.file}`)
+  }
+})
+
+module.exports = testReporter