Skip to content

Commit 4f896c2

Browse files
kugtong33novemberborn
authored andcommittedJan 25, 2018
Make t.log() behave more like console.log()
1 parent f00f3c4 commit 4f896c2

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed
 

‎index.js.flow

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type TestContext = AssertContext & {
8484
title: string;
8585
plan(count: number): void;
8686
skip: AssertContext;
87-
log(message: string): void;
87+
log(...values: Array<any>): void;
8888
};
8989
type ContextualTestContext = TestContext & { context: any; };
9090
type ContextualCallbackTestContext = TestContext & { context: any; end(): void; };

‎lib/assert.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,16 @@ function wrapAssertions(callbacks) {
130130
}
131131
},
132132

133-
log(text) {
134-
log(this, text);
133+
log() {
134+
const args = Array.from(arguments, value => {
135+
return typeof value === 'string' ?
136+
value :
137+
concordance.format(value, concordanceOptions);
138+
});
139+
140+
if (args.length > 0) {
141+
log(this, args.join(' '));
142+
}
135143
},
136144

137145
deepEqual(actual, expected, message) {

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,9 @@ Plan how many assertion there are in the test. The test will fail if the actual
890890

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

893-
###### `t.log(message)`
893+
###### `t.log(...values)`
894894

895-
Print a log message contextually alongside the test result instead of immediately printing it to `stdout` like `console.log`.
895+
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.
896896

897897
## Assertions
898898

‎test/flow-types/log.js.flow

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* @flow */
2+
3+
const test = require('../../index.js.flow');
4+
5+
test('log', t => {
6+
t.pass();
7+
t.log({object: true}, 42, ['array'], false, new Date(), new Map());
8+
})

‎test/test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -741,13 +741,19 @@ test('log from tests', t => {
741741
a.log('a log message from a test');
742742
t.true(true);
743743
a.log('another log message from a test');
744+
a.log({b: 1, c: {d: 2}}, 'complex log', 5, 5.1);
745+
a.log();
744746
}, null, r => {
745747
result = r;
746748
}).run();
747749

748750
t.deepEqual(
749751
result.result.logs,
750-
['a log message from a test', 'another log message from a test']
752+
[
753+
'a log message from a test',
754+
'another log message from a test',
755+
'{\n b: 1,\n c: {\n d: 2,\n },\n} complex log 5 5.1'
756+
]
751757
);
752758

753759
t.end();

‎types/base.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ export interface TestContext extends AssertContext {
104104

105105
skip: AssertContext;
106106
/**
107-
* Print a log message contextually alongside the test result instead of immediately printing it to stdout like console.log.
107+
* Log values contextually alongside the test result instead of immediately printing them to `stdout`.
108108
*/
109-
log(message: string): void;
109+
log(...values: any[]): void;
110110
}
111111
export interface CallbackTestContext extends TestContext {
112112
/**

0 commit comments

Comments
 (0)
Please sign in to comment.