Skip to content

Commit

Permalink
Make t.log() behave more like console.log()
Browse files Browse the repository at this point in the history
  • Loading branch information
kugtong33 authored and novemberborn committed Jan 25, 2018
1 parent f00f3c4 commit 4f896c2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type TestContext = AssertContext & {
title: string;
plan(count: number): void;
skip: AssertContext;
log(message: string): void;
log(...values: Array<any>): void;
};
type ContextualTestContext = TestContext & { context: any; };
type ContextualCallbackTestContext = TestContext & { context: any; end(): void; };
Expand Down
12 changes: 10 additions & 2 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,16 @@ function wrapAssertions(callbacks) {
}
},

log(text) {
log(this, text);
log() {
const args = Array.from(arguments, value => {
return typeof value === 'string' ?
value :
concordance.format(value, concordanceOptions);
});

if (args.length > 0) {
log(this, args.join(' '));
}
},

deepEqual(actual, expected, message) {
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,9 @@ Plan how many assertion there are in the test. The test will fail if the actual

End the test. Only works with `test.cb()`.

###### `t.log(message)`
###### `t.log(...values)`

Print a log message contextually alongside the test result instead of immediately printing it to `stdout` like `console.log`.
Log values contextually alongside the test result instead of immediately printing them to `stdout`. Behaves somewhat like `console.log`, but without support for placeholder tokens.

## Assertions

Expand Down
8 changes: 8 additions & 0 deletions test/flow-types/log.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* @flow */

const test = require('../../index.js.flow');

test('log', t => {
t.pass();
t.log({object: true}, 42, ['array'], false, new Date(), new Map());
})
8 changes: 7 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,13 +741,19 @@ test('log from tests', t => {
a.log('a log message from a test');
t.true(true);
a.log('another log message from a test');
a.log({b: 1, c: {d: 2}}, 'complex log', 5, 5.1);
a.log();
}, null, r => {
result = r;
}).run();

t.deepEqual(
result.result.logs,
['a log message from a test', 'another log message from a test']
[
'a log message from a test',
'another log message from a test',
'{\n b: 1,\n c: {\n d: 2,\n },\n} complex log 5 5.1'
]
);

t.end();
Expand Down
4 changes: 2 additions & 2 deletions types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export interface TestContext extends AssertContext {

skip: AssertContext;
/**
* Print a log message contextually alongside the test result instead of immediately printing it to stdout like console.log.
* Log values contextually alongside the test result instead of immediately printing them to `stdout`.
*/
log(message: string): void;
log(...values: any[]): void;
}
export interface CallbackTestContext extends TestContext {
/**
Expand Down

0 comments on commit 4f896c2

Please sign in to comment.