Formatted TAP output like Mocha's spec reporter
npx @tessl/cli install tessl/npm-tap-spec@5.0.0tap-spec is a TAP (Test Anything Protocol) output formatter that transforms plain TAP test results into a visually appealing, Mocha-style spec reporter format. It serves as a streaming transform tool that can be piped from TAP-producing test runners like tape, providing colored output with symbols, proper indentation, and timing information.
npm install tap-spec --save-devconst tapSpec = require('tap-spec');For ES modules:
import tapSpec from 'tap-spec';const tapSpec = require('tap-spec');
const test = require('tape');
// Create a formatter with default options
const formatter = tapSpec();
// Pipe TAP output through the formatter
test.createStream()
.pipe(formatter)
.pipe(process.stdout);# Via npm scripts in package.json
{
"scripts": {
"test": "tape test.js | tap-spec"
}
}
# Direct command line usage
tape test/index.js | tap-spec
node test.js | tspec
# With testling
testling test/index.js | tap-specCreates a transform stream that formats TAP input into spec-style output with colors, symbols, and proper indentation.
/**
* Creates a TAP formatter transform stream
* @param {Object} [spec] - Configuration options
* @param {string} [spec.padding=' '] - String used for indenting output lines
* @returns {Stream} Transform stream (duplex) that formats TAP input
*/
function tapSpec(spec) {
// Returns a duplex stream with parser input and formatted output
}⚠️ Important Note on Process Termination:
When used programmatically, tap-spec may call process.exit(1) directly in the following conditions:
This means the entire Node.js process will terminate. If you need to handle these conditions gracefully in your application, consider:
failed property to detect failures before process terminationConfiguration Options:
interface TapSpecOptions {
/** String used for indenting output lines (default: ' ') */
padding?: string;
}Stream Properties:
interface TapSpecStream extends Stream {
/** Set to true when test failures are encountered */
failed?: boolean;
}Usage Examples:
const tapSpec = require('tap-spec');
// Default formatting
const defaultFormatter = tapSpec();
// Custom padding
const customFormatter = tapSpec({ padding: ' ' });
// Check for failures
const formatter = tapSpec();
tapInput.pipe(formatter).pipe(process.stdout);
formatter.on('end', () => {
if (formatter.failed) {
process.exit(1);
}
});Two equivalent command-line interfaces for formatting TAP output.
# Primary command
tap-spec
# Alias command
tspecBoth commands:
Usage:
# Pipe from test runner
tape test.js | tap-spec
node test.js | tspec
# Via npm scripts
npm test | tap-spec
# With other TAP producers
tap test.js | tap-specThe formatter produces Mocha-style spec output with:
# SKIP directive)Example Output:
my test suite
✓ should pass this assertion
✓ should also pass this one
✗ should fail this assertion
-------------------------
Error details here...
Failed Tests: There was 1 failure
1) my test suite
✗ should fail this assertion
total: 3
passing: 2
failing: 1
duration: 125msStream Behavior:
failed property to true when test failures occurProcess Termination (Both Programmatic and CLI):
process.exit(1) when no test plans are found (upstream failure)process.exit(1) when no tests and no assertions are detectedFailure Detection:
failed property on the returned stream to detect test failuresThe package relies on several key dependencies that define its functionality: