From 12142127bf2caa2ce17ac10bfa605eb5bc7d8fd4 Mon Sep 17 00:00:00 2001 From: megamoth Date: Thu, 25 Jan 2018 00:05:21 +0800 Subject: [PATCH 1/5] Let t.log behaves like console.log --- lib/assert.js | 11 +++++++++-- test/test.js | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 18009b97d..57598f887 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -122,8 +122,15 @@ function wrapAssertions(callbacks) { } }, - log(text) { - log(this, text); + log(...args) { + args.forEach((value, index) => { + args[index] = typeof value === 'string' ? + value : concordance.format(value, concordanceOptions); + }); + + if (args.length > 0) { + log(this, args.join(' ')); + } }, deepEqual(actual, expected, message) { diff --git a/test/test.js b/test/test.js index c2db8067c..d3d45b44f 100644 --- a/test/test.js +++ b/test/test.js @@ -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); + t.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(); From 0548a36c70103062c754ab486579e674a656e075 Mon Sep 17 00:00:00 2001 From: megamoth Date: Thu, 25 Jan 2018 00:41:52 +0800 Subject: [PATCH 2/5] Convert t.log arguments object into array --- lib/assert.js | 3 ++- test/test.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 57598f887..b17a4eff5 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -122,7 +122,8 @@ function wrapAssertions(callbacks) { } }, - log(...args) { + log() { + const args = Array.prototype.slice.call(arguments); args.forEach((value, index) => { args[index] = typeof value === 'string' ? value : concordance.format(value, concordanceOptions); diff --git a/test/test.js b/test/test.js index d3d45b44f..68ff0c43f 100644 --- a/test/test.js +++ b/test/test.js @@ -742,7 +742,7 @@ test('log from tests', t => { t.true(true); a.log('another log message from a test'); a.log({b: 1, c: {d: 2}}, 'complex log', 5, 5.1); - t.log(); + a.log(); }, null, r => { result = r; }).run(); From c3b747e9ce9131cab6e06d88a9e8ee1d478c45e6 Mon Sep 17 00:00:00 2001 From: megamoth Date: Thu, 25 Jan 2018 09:16:03 +0800 Subject: [PATCH 3/5] Use Array.from for better arguments mapping --- lib/assert.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index b17a4eff5..1a99f337c 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -123,11 +123,10 @@ function wrapAssertions(callbacks) { }, log() { - const args = Array.prototype.slice.call(arguments); - args.forEach((value, index) => { - args[index] = typeof value === 'string' ? - value : concordance.format(value, concordanceOptions); - }); + const args = Array.from(arguments, value => + typeof value === 'string' ? + value : concordance.format(value, concordanceOptions) + ); if (args.length > 0) { log(this, args.join(' ')); From 12f12e9b3a44d88690ed96faa091c79ea8945c3d Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Thu, 25 Jan 2018 14:47:12 +0000 Subject: [PATCH 4/5] Reformat map function --- lib/assert.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 1a99f337c..301356c3b 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -123,10 +123,11 @@ function wrapAssertions(callbacks) { }, log() { - const args = Array.from(arguments, value => - typeof value === 'string' ? - value : concordance.format(value, concordanceOptions) - ); + const args = Array.from(arguments, value => { + return typeof value === 'string' ? + value : + concordance.format(value, concordanceOptions); + }); if (args.length > 0) { log(this, args.join(' ')); From dbf864379d7ef52aeb787ea8172774f77e120ef0 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Thu, 25 Jan 2018 15:33:02 +0000 Subject: [PATCH 5/5] Update README and type definitions --- index.js.flow | 2 +- readme.md | 4 ++-- test/flow-types/log.js.flow | 8 ++++++++ types/base.d.ts | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 test/flow-types/log.js.flow diff --git a/index.js.flow b/index.js.flow index e0bda9aef..d21470695 100644 --- a/index.js.flow +++ b/index.js.flow @@ -84,7 +84,7 @@ type TestContext = AssertContext & { title: string; plan(count: number): void; skip: AssertContext; - log(message: string): void; + log(...values: Array): void; }; type ContextualTestContext = TestContext & { context: any; }; type ContextualCallbackTestContext = TestContext & { context: any; end(): void; }; diff --git a/readme.md b/readme.md index 825e83253..ff4b58d35 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/flow-types/log.js.flow b/test/flow-types/log.js.flow new file mode 100644 index 000000000..0c0c94323 --- /dev/null +++ b/test/flow-types/log.js.flow @@ -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()); +}) diff --git a/types/base.d.ts b/types/base.d.ts index 84a79553e..774db3fa1 100644 --- a/types/base.d.ts +++ b/types/base.d.ts @@ -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 { /**