|
| 1 | +/* |
| 2 | + * Copyright 2024 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +// This file provides a custom test reporter for the native test runner |
| 7 | +// included in Node.js >=18. The default `spec` reporter writes too much |
| 8 | +// information to be usable in CI, and the `dot` reporter hides which tests |
| 9 | +// failed. This custom reporter outputs nothing for successful tests, and |
| 10 | +// outputs the failing test file when any failing test has occurred. |
| 11 | +// |
| 12 | +// See https://nodejs.org/api/test.html#custom-reporters. |
| 13 | +'use strict' |
| 14 | + |
| 15 | +const { Transform } = require('node:stream') |
| 16 | +const testReporter = new Transform({ |
| 17 | + writableObjectMode: true, |
| 18 | + transform(event, encoding, callback) { |
| 19 | + if (event.type !== 'test:fail') { |
| 20 | + // We don't want to write out anything for any cases other than the |
| 21 | + // failure case. |
| 22 | + return callback(null, null) |
| 23 | + } |
| 24 | + |
| 25 | + // Once v18 has been dropped, we might want to revisit the output of |
| 26 | + // failure cases. The `event` object is supposed to provide things like |
| 27 | + // the failing line number and column, along with the failing test name. |
| 28 | + // But on v18, we seem to only get `1` for both line and column, and the |
| 29 | + // test name gets set to the `file`. So there isn't really any point in |
| 30 | + // trying to provide more useful reports here while we need to support v18. |
| 31 | + // |
| 32 | + // The issue may also stem from the current test suites still being based |
| 33 | + // on `tap`. Once we are able to migrate the actual test code to `node:test` |
| 34 | + // we should revisit this reporter to determine if we can improve it. |
| 35 | + // |
| 36 | + // See https://nodejs.org/api/test.html#event-testfail. |
| 37 | + callback(null, `failed: ${event.data.file}`) |
| 38 | + } |
| 39 | +}) |
| 40 | + |
| 41 | +module.exports = testReporter |
0 commit comments