Skip to content

Commit

Permalink
Use supertap to generate TAP output (#1610)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Dec 17, 2017
1 parent 4124d77 commit 965cbc6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 66 deletions.
89 changes: 25 additions & 64 deletions lib/reporters/tap.js
@@ -1,8 +1,6 @@
'use strict';
const format = require('util').format;
const indentString = require('indent-string');
const supertap = require('supertap');
const stripAnsi = require('strip-ansi');
const yaml = require('js-yaml');

function dumpError(error, includeMessage) {
const obj = Object.assign({}, error.object);
Expand Down Expand Up @@ -32,7 +30,7 @@ function dumpError(error, includeMessage) {
obj.at = error.stack.split('\n')[0];
}

return ` ---\n${indentString(yaml.safeDump(obj).trim(), 4)}\n ...`;
return obj;
}

class TapReporter {
Expand All @@ -41,79 +39,42 @@ class TapReporter {
}

start() {
return 'TAP version 13';
return supertap.start();
}

test(test) {
const output = [];

let directive = '';
const passed = test.todo ? 'not ok' : 'ok';

if (test.todo) {
directive = '# TODO';
} else if (test.skip) {
directive = '# SKIP';
}

const title = stripAnsi(test.title);

const appendLogs = () => {
if (test.logs) {
test.logs.forEach(log => {
const logLines = indentString(log, 4);
const logLinesWithLeadingFigure = logLines.replace(
/^ {4}/,
' * '
);

output.push(logLinesWithLeadingFigure);
});
}
};

output.push(`# ${title}`);

if (test.error) {
output.push(format('not ok %d - %s', ++this.i, title));
appendLogs();
output.push(dumpError(test.error, true));
} else {
output.push(format('%s %d - %s %s', passed, ++this.i, title, directive).trim());
appendLogs();
}

return output.join('\n');
return supertap.test(test.title, {
passed: !test.error,
index: ++this.i,
todo: test.todo,
skip: test.skip,
comment: test.logs,
error: test.error ? dumpError(test.error, true) : null
});
}

unhandledError(err) {
const output = [
`# ${err.message}`,
format('not ok %d - %s', ++this.i, err.message)
];
let error;

// AvaErrors don't have stack traces
if (err.type !== 'exception' || err.name !== 'AvaError') {
output.push(dumpError(err, false));
error = dumpError(err, false);
}

return output.join('\n');
return supertap.test(err.message, {
passed: false,
index: ++this.i,
error
});
}

finish(runStatus) {
const output = [
'',
'1..' + (runStatus.passCount + runStatus.failCount + runStatus.skipCount),
'# tests ' + (runStatus.passCount + runStatus.failCount + runStatus.skipCount),
'# pass ' + runStatus.passCount
];

if (runStatus.skipCount > 0) {
output.push(`# skip ${runStatus.skipCount}`);
}

output.push('# fail ' + (runStatus.failCount + runStatus.rejectionCount + runStatus.exceptionCount), '');

return output.join('\n');
return supertap.finish({
passed: runStatus.passCount,
failed: runStatus.failCount,
skipped: runStatus.skipCount,
crashed: runStatus.rejectionCount + runStatus.exceptionCount
});
}

write(str) {
Expand Down
42 changes: 41 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -111,7 +111,6 @@
"is-obj": "^1.0.0",
"is-observable": "^1.0.0",
"is-promise": "^2.1.0",
"js-yaml": "^3.8.2",
"last-line-stream": "^1.0.0",
"lodash.clonedeepwith": "^4.5.0",
"lodash.debounce": "^4.0.3",
Expand Down Expand Up @@ -139,6 +138,7 @@
"stack-utils": "^1.0.1",
"strip-ansi": "^4.0.0",
"strip-bom-buf": "^1.0.0",
"supertap": "^1.0.0",
"supports-color": "^5.0.0",
"time-require": "^0.1.2",
"trim-off-newlines": "^1.0.1",
Expand Down

0 comments on commit 965cbc6

Please sign in to comment.