Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 add log_processor config option #1463

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ For improved ergonomics, TAP reporter does not actually strictly adhere to the S
}
```

By default, the TAP reporter outputs the result of `JSON.stringify()` for any log content that is not a `String`. You can override this behavior by specifying a function for `tap_log_processor`.

```js
{
"tap_log_processor": function(log) { return log.toString(); }
}
```

## Other Test Reporters

Testem has other test reporters besides TAP: `dot`, `xunit` and `teamcity`. You can use the `-R` to specify them
Expand Down
3 changes: 2 additions & 1 deletion lib/reporters/tap_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = class TapReporter {
this.results = [];
this.errors = [];
this.logs = [];
this.logProcessor = config.get('tap_log_processor');
}

report(prefix, data) {
Expand Down Expand Up @@ -56,7 +57,7 @@ module.exports = class TapReporter {
*/
display(prefix, result) {
if (this.willDisplay(result)) {
this.out.write(displayutils.resultString(this.id++, prefix, result, this.quietLogs, this.strictSpecCompliance));
this.out.write(displayutils.resultString(this.id++, prefix, result, this.quietLogs, this.strictSpecCompliance, this.logProcessor));
}
}

Expand Down
12 changes: 8 additions & 4 deletions lib/utils/displayutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function resultDisplay(id, prefix, result, strictSpecCompliance) {
return output;
}

function yamlDisplay(err, logs) {
function yamlDisplay(err, logs, logProcessor) {
let testLogs;
let failed = Object.keys(err || {})
.filter(key => key !== 'passed')
Expand All @@ -68,7 +68,11 @@ function yamlDisplay(err, logs) {
let logLine;
if (strutils.isString(log)) {
logLine = log;
} else {
}
else if (logProcessor) {
logLine = logProcessor(log);
}
else {
logLine = JSON.stringify(log);
}
return strutils.indent(logLine);
Expand All @@ -84,10 +88,10 @@ function yamlDisplay(err, logs) {
].join('\n'));
}

function resultString(id, prefix, result, quietLogs, strictSpecCompliance) {
function resultString(id, prefix, result, quietLogs, strictSpecCompliance, logProcessor) {
let string = resultDisplay(id, prefix, result, strictSpecCompliance) + '\n';
if (result.error || (!quietLogs && result.logs && result.logs.length)) {
string += yamlDisplay(result.error, result.logs) + '\n';
string += yamlDisplay(result.error, result.logs, logProcessor) + '\n';
}
return string;
}
Expand Down
72 changes: 69 additions & 3 deletions tests/ci/reporter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ describe('test reporters', function() {
logs: [],
runDuration: 0,
});
reporter.report('phantomjs', {
name: 'it logs something other than a string',
passed: true,
logs: [{text: 'ye olde texte'}],
runDuration: 3,
});
reporter.finish();
assert.deepEqual(stream.read().toString().split('\n'), [
'ok 1 phantomjs - [3 ms] - it does stuff',
Expand All @@ -82,10 +88,15 @@ describe('test reporters', function() {
' some log',
' ...',
'skip 2 phantomjs - [0 ms] - it is skipped',
'ok 3 phantomjs - [3 ms] - it logs something other than a string',
' ---',
' browser log: |',
' {"text":"ye olde texte"}',
' ...',
'',
'1..2',
'# tests 2',
'# pass 1',
'1..3',
'# tests 3',
'# pass 2',
'# skip 1',
'# todo 0',
'# fail 0',
Expand Down Expand Up @@ -615,6 +626,61 @@ describe('test reporters', function() {
assert.equal(reporter.willDisplay({error: true}), true);
});
});

context('with log processor', function() {
beforeEach(function() {
config = new Config('ci', {
tap_log_processor: function(log) {
return `yee ${log.text}`; } });
});

it('uses log processor', function() {
var reporter = new TapReporter(false, stream, config);
reporter.report('phantomjs', {
name: 'it does stuff',
passed: true,
logs: ['some log'],
runDuration: 3,
});
reporter.report('phantomjs', {
name: 'it is skipped',
skipped: true,
logs: [],
runDuration: 0,
});
reporter.report('phantomjs', {
name: 'it logs something other than a string',
passed: true,
logs: [{text: 'ye olde texte'}],
runDuration: 3,
});
reporter.finish();
assert.deepEqual(stream.read().toString().split('\n'), [
'ok 1 phantomjs - [3 ms] - it does stuff',
' ---',
' browser log: |',
' some log',
' ...',
'skip 2 phantomjs - [0 ms] - it is skipped',
'ok 3 phantomjs - [3 ms] - it logs something other than a string',
' ---',
' browser log: |',
' yee ye olde texte',
' ...',
'',
'1..3',
'# tests 3',
'# pass 2',
'# skip 1',
'# todo 0',
'# fail 0',
'',
'# ok',
''
]);
});
});

});

describe('dot reporter', function() {
Expand Down
Loading