Skip to content

Commit

Permalink
Add colored test output
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Oct 23, 2020
1 parent ca62002 commit 13176bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/TestSuiteRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class TestSuiteRunner {
for (const result of results) {
if (result.ok) {
success++;
stdout.write(`${LogSymbols.success} ${result.test.name} (${result.test.uri})${result.duration ? ` \x1b[90m${result.duration}ms\x1b[0m` : ''}\n`);
stdout.write(`${LogSymbols.success} ${result.test.name} (${result.test.uri})${result.duration ? ` ${Util.withColor(`${result.duration}ms`, Util.COLOR_GRAY)}` : ''}\n`);
} else {
if (result.skipped) {
skipped++;
Expand All @@ -157,10 +157,10 @@ export class TestSuiteRunner {
if (!compact) {
for (const result of failedTests) {
stdout.write(`
${LogSymbols.error} ${result.test.name}
${LogSymbols.error} ${Util.withColor(result.test.name, Util.COLOR_RED)}
${result.test.comment || ''}
${'test' in result.error ? result.error : result.error.stack}
More info: ${result.test.uri}
${Util.withColor(`More info: ${result.test.uri}`, Util.COLOR_BLUE)}
`);
}
Expand Down
18 changes: 18 additions & 0 deletions lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ const streamifyString = require('streamify-string');
*/
export class Util {

public static COLOR_RESET: string = '\x1b[0m';
public static COLOR_RED: string = '\x1b[31m';
public static COLOR_GREEN: string = '\x1b[32m';
public static COLOR_YELLOW: string = '\x1b[33m';
public static COLOR_BLUE: string = '\x1b[34m';
public static COLOR_MAGENTA: string = '\x1b[35m';
public static COLOR_CYAN: string = '\x1b[36m';
public static COLOR_GRAY: string = '\x1b[90m';

protected static readonly EXTENSION_TO_CONTENTTYPE: {[extension: string]: string} = {
jsonld: 'application/ld+json',
nq: 'application/n-quads',
Expand Down Expand Up @@ -190,6 +199,15 @@ export class Util {
return 'http://opensource.org/licenses/' + license;
}

/**
* Return a string in a given color
* @param str The string that should be printed in
* @param color A given color
*/
public static withColor(str: any, color: string) {
return `${color}${str}${Util.COLOR_RESET}`;
}

}

/**
Expand Down
13 changes: 7 additions & 6 deletions test/TestSuiteRunner-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import "jest-rdf";
import * as LogSymbols from "log-symbols";
import {PassThrough} from "stream";
import {ITestSuiteConfig, TestSuiteRunner} from "../lib/TestSuiteRunner";
import { Util } from '../lib/Util';

// tslint:disable:no-var-requires
const stringifyStream = require('stream-to-string');
Expand Down Expand Up @@ -346,10 +347,10 @@ describe('TestSuiteRunner', () => {
${LogSymbols.success} Test2 (http://ex.org/test2)
${LogSymbols.error} Test3 (http://ex.org/test3)
${LogSymbols.error} Test3
${LogSymbols.error} ${Util.withColor('Test3', Util.COLOR_RED)}
Error: Fail
More info: http://ex.org/test3
${Util.withColor('More info: http://ex.org/test3', Util.COLOR_BLUE)}
${LogSymbols.error} 2 / 3 tests succeeded!
`);
Expand All @@ -365,10 +366,10 @@ ${LogSymbols.info} Test2 (http://ex.org/test2)
${LogSymbols.info} Test3 (http://ex.org/test3)
${LogSymbols.error} Test3 (http://ex.org/test3)
${LogSymbols.error} Test3
${LogSymbols.error} ${Util.withColor('Test3', Util.COLOR_RED)}
Error: Fail
More info: http://ex.org/test3
${Util.withColor('More info: http://ex.org/test3', Util.COLOR_BLUE)}
${LogSymbols.error} 3 / 4 tests succeeded! (skipped 2)
`);
Expand All @@ -384,10 +385,10 @@ ${LogSymbols.info} Test2 (http://ex.org/test2)
${LogSymbols.info} Test3 (http://ex.org/test3)
${LogSymbols.error} Test3 (http://ex.org/test3)
${LogSymbols.error} Test3
${LogSymbols.error} ${Util.withColor('Test3', Util.COLOR_RED)}
MYSTACK
More info: http://ex.org/test3
${Util.withColor('More info: http://ex.org/test3', Util.COLOR_BLUE)}
${LogSymbols.error} 3 / 4 tests succeeded! (skipped 2)
`);
Expand Down

0 comments on commit 13176bd

Please sign in to comment.