Skip to content

Make t.log() behave like console.log() #1653

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

Merged
merged 5 commits into from
Jan 25, 2018
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
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 @@ -122,8 +122,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