From 5f64bc2941c1602cb55f24064d1542eecfafb592 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Mon, 11 Feb 2019 23:19:05 +0000 Subject: [PATCH 01/13] add console.log ref --- lib/reporters/base.js | 21 +++++++++++++-------- lib/reporters/doc.js | 25 +++++++++++++++---------- lib/reporters/dot.js | 7 ++++++- lib/reporters/list.js | 13 +++++++++---- lib/reporters/progress.js | 7 ++++++- lib/reporters/spec.js | 19 ++++++++++++------- lib/reporters/xunit.js | 7 ++++++- 7 files changed, 67 insertions(+), 32 deletions(-) diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 3736c018ca..c321552680 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -27,6 +27,11 @@ exports = module.exports = Base; var isatty = tty.isatty(1) && tty.isatty(2); +/** + * Save log references to avoid tests interfering (see GH-3604). + */ +var println = console.log; + /** * Enable coloring by default, except in the browser interface. */ @@ -192,7 +197,7 @@ var generateDiff = (exports.generateDiff = function(actual, expected) { * Error property */ exports.list = function(failures) { - console.log(); + println(); failures.forEach(function(test, i) { // format var fmt = @@ -253,7 +258,7 @@ exports.list = function(failures) { testTitle += str; }); - console.log(fmt, i + 1, testTitle, msg, stack); + println(fmt, i + 1, testTitle, msg, stack); }); }; @@ -308,7 +313,7 @@ Base.prototype.epilogue = function() { var stats = this.stats; var fmt; - console.log(); + println(); // passes fmt = @@ -316,26 +321,26 @@ Base.prototype.epilogue = function() { color('green', ' %d passing') + color('light', ' (%s)'); - console.log(fmt, stats.passes || 0, milliseconds(stats.duration)); + println(fmt, stats.passes || 0, milliseconds(stats.duration)); // pending if (stats.pending) { fmt = color('pending', ' ') + color('pending', ' %d pending'); - console.log(fmt, stats.pending); + println(fmt, stats.pending); } // failures if (stats.failures) { fmt = color('fail', ' %d failing'); - console.log(fmt, stats.failures); + println(fmt, stats.failures); Base.list(this.failures); - console.log(); + println(); } - console.log(); + println(); }; /** diff --git a/lib/reporters/doc.js b/lib/reporters/doc.js index efcb2d0caf..c47e4ddbfd 100644 --- a/lib/reporters/doc.js +++ b/lib/reporters/doc.js @@ -20,6 +20,11 @@ var EVENT_SUITE_END = constants.EVENT_SUITE_END; exports = module.exports = Doc; +/** + * Save log references to avoid tests interfering (see GH-3604). + */ +var println = console.log; + /** * Constructs a new `Doc` reporter instance. * @@ -44,41 +49,41 @@ function Doc(runner, options) { return; } ++indents; - console.log('%s
', indent()); + println('%s
', indent()); ++indents; - console.log('%s

%s

', indent(), utils.escape(suite.title)); - console.log('%s
', indent()); + println('%s

%s

', indent(), utils.escape(suite.title)); + println('%s
', indent()); }); runner.on(EVENT_SUITE_END, function(suite) { if (suite.root) { return; } - console.log('%s
', indent()); + println('%s
', indent()); --indents; - console.log('%s
', indent()); + println('%s
', indent()); --indents; }); runner.on(EVENT_TEST_PASS, function(test) { - console.log('%s
%s
', indent(), utils.escape(test.title)); + println('%s
%s
', indent(), utils.escape(test.title)); var code = utils.escape(utils.clean(test.body)); - console.log('%s
%s
', indent(), code); + println('%s
%s
', indent(), code); }); runner.on(EVENT_TEST_FAIL, function(test, err) { - console.log( + println( '%s
%s
', indent(), utils.escape(test.title) ); var code = utils.escape(utils.clean(test.body)); - console.log( + println( '%s
%s
', indent(), code ); - console.log('%s
%s
', indent(), utils.escape(err)); + println('%s
%s
', indent(), utils.escape(err)); }); } diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index c4c5ce5d92..fa028ac9df 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -21,6 +21,11 @@ var EVENT_RUN_END = constants.EVENT_RUN_END; exports = module.exports = Dot; +/** + * Save log references to avoid tests interfering (see GH-3604). + */ +var println = console.log; + /** * Constructs a new `Dot` reporter instance. * @@ -68,7 +73,7 @@ function Dot(runner, options) { }); runner.once(EVENT_RUN_END, function() { - console.log(); + println(); self.epilogue(); }); } diff --git a/lib/reporters/list.js b/lib/reporters/list.js index cdf4279644..7539426617 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -24,6 +24,11 @@ var cursor = Base.cursor; exports = module.exports = List; +/** + * Save log references to avoid tests interfering (see GH-3604). + */ +var println = console.log; + /** * Constructs a new `List` reporter instance. * @@ -41,7 +46,7 @@ function List(runner, options) { var n = 0; runner.on(EVENT_RUN_BEGIN, function() { - console.log(); + println(); }); runner.on(EVENT_TEST_BEGIN, function(test) { @@ -50,7 +55,7 @@ function List(runner, options) { runner.on(EVENT_TEST_PENDING, function(test) { var fmt = color('checkmark', ' -') + color('pending', ' %s'); - console.log(fmt, test.fullTitle()); + println(fmt, test.fullTitle()); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -59,12 +64,12 @@ function List(runner, options) { color('pass', ' %s: ') + color(test.speed, '%dms'); cursor.CR(); - console.log(fmt, test.fullTitle(), test.duration); + println(fmt, test.fullTitle(), test.duration); }); runner.on(EVENT_TEST_FAIL, function(test) { cursor.CR(); - console.log(color('fail', ' %d) %s'), ++n, test.fullTitle()); + println(color('fail', ' %d) %s'), ++n, test.fullTitle()); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 0432e4fb0a..6c2c09088d 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -21,6 +21,11 @@ var cursor = Base.cursor; exports = module.exports = Progress; +/** + * Save log references to avoid tests interfering (see GH-3604). + */ +var println = console.log; + /** * General progress bar color. */ @@ -91,7 +96,7 @@ function Progress(runner, options) { // and the failures if any runner.once(EVENT_RUN_END, function() { cursor.show(); - console.log(); + println(); self.epilogue(); }); } diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index e1ae95e92d..b6286738db 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -24,6 +24,11 @@ var color = Base.color; exports = module.exports = Spec; +/** + * Save log references to avoid tests interfering (see GH-3604). + */ +var println = console.log; + /** * Constructs a new `Spec` reporter instance. * @@ -46,24 +51,24 @@ function Spec(runner, options) { } runner.on(EVENT_RUN_BEGIN, function() { - console.log(); + println(); }); runner.on(EVENT_SUITE_BEGIN, function(suite) { ++indents; - console.log(color('suite', '%s%s'), indent(), suite.title); + println(color('suite', '%s%s'), indent(), suite.title); }); runner.on(EVENT_SUITE_END, function() { --indents; if (indents === 1) { - console.log(); + println(); } }); runner.on(EVENT_TEST_PENDING, function(test) { var fmt = indent() + color('pending', ' - %s'); - console.log(fmt, test.title); + println(fmt, test.title); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -73,19 +78,19 @@ function Spec(runner, options) { indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s'); - console.log(fmt, test.title); + println(fmt, test.title); } else { fmt = indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s') + color(test.speed, ' (%dms)'); - console.log(fmt, test.title, test.duration); + println(fmt, test.title, test.duration); } }); runner.on(EVENT_TEST_FAIL, function(test) { - console.log(indent() + color('fail', ' %d) %s'), ++n, test.title); + println(indent() + color('fail', ' %d) %s'), ++n, test.title); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js index 09b32f1ca7..50cac91aab 100644 --- a/lib/reporters/xunit.js +++ b/lib/reporters/xunit.js @@ -27,6 +27,11 @@ var escape = utils.escape; */ var Date = global.Date; +/** + * Save log references to avoid tests interfering (see GH-3604). + */ +var println = console.log; + /** * Expose `XUnit`. */ @@ -142,7 +147,7 @@ XUnit.prototype.write = function(line) { } else if (typeof process === 'object' && process.stdout) { process.stdout.write(line + '\n'); } else { - console.log(line); + println(line); } }; From fff01ff9862eb419b0264c101e8d5a1ea1991cac Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Wed, 13 Feb 2019 12:33:42 +0000 Subject: [PATCH 02/13] inherit println --- lib/reporters/base.js | 12 ++++++------ lib/reporters/doc.js | 26 +++++++++++--------------- lib/reporters/dot.js | 2 +- lib/reporters/list.js | 8 ++++---- lib/reporters/progress.js | 7 +------ lib/reporters/spec.js | 14 +++++++------- lib/reporters/xunit.js | 7 +------ 7 files changed, 31 insertions(+), 45 deletions(-) diff --git a/lib/reporters/base.js b/lib/reporters/base.js index c321552680..fc34e3e75f 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -313,7 +313,7 @@ Base.prototype.epilogue = function() { var stats = this.stats; var fmt; - println(); + this.println(); // passes fmt = @@ -321,26 +321,26 @@ Base.prototype.epilogue = function() { color('green', ' %d passing') + color('light', ' (%s)'); - println(fmt, stats.passes || 0, milliseconds(stats.duration)); + this.println(fmt, stats.passes || 0, milliseconds(stats.duration)); // pending if (stats.pending) { fmt = color('pending', ' ') + color('pending', ' %d pending'); - println(fmt, stats.pending); + this.println(fmt, stats.pending); } // failures if (stats.failures) { fmt = color('fail', ' %d failing'); - println(fmt, stats.failures); + this.println(fmt, stats.failures); Base.list(this.failures); - println(); + this.println(); } - println(); + this.println(); }; /** diff --git a/lib/reporters/doc.js b/lib/reporters/doc.js index c47e4ddbfd..117d74d43a 100644 --- a/lib/reporters/doc.js +++ b/lib/reporters/doc.js @@ -20,11 +20,6 @@ var EVENT_SUITE_END = constants.EVENT_SUITE_END; exports = module.exports = Doc; -/** - * Save log references to avoid tests interfering (see GH-3604). - */ -var println = console.log; - /** * Constructs a new `Doc` reporter instance. * @@ -39,6 +34,7 @@ function Doc(runner, options) { Base.call(this, runner, options); var indents = 2; + var self = this; function indent() { return Array(indents).join(' '); @@ -49,41 +45,41 @@ function Doc(runner, options) { return; } ++indents; - println('%s
', indent()); + self.println('%s
', indent()); ++indents; - println('%s

%s

', indent(), utils.escape(suite.title)); - println('%s
', indent()); + self.println('%s

%s

', indent(), utils.escape(suite.title)); + self.println('%s
', indent()); }); runner.on(EVENT_SUITE_END, function(suite) { if (suite.root) { return; } - println('%s
', indent()); + self.println('%s
', indent()); --indents; - println('%s
', indent()); + self.println('%s
', indent()); --indents; }); runner.on(EVENT_TEST_PASS, function(test) { - println('%s
%s
', indent(), utils.escape(test.title)); + self.println('%s
%s
', indent(), utils.escape(test.title)); var code = utils.escape(utils.clean(test.body)); - println('%s
%s
', indent(), code); + self.println('%s
%s
', indent(), code); }); runner.on(EVENT_TEST_FAIL, function(test, err) { - println( + self.println( '%s
%s
', indent(), utils.escape(test.title) ); var code = utils.escape(utils.clean(test.body)); - println( + self.println( '%s
%s
', indent(), code ); - println('%s
%s
', indent(), utils.escape(err)); + self.println('%s
%s
', indent(), utils.escape(err)); }); } diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index fa028ac9df..bcb92697d2 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -73,7 +73,7 @@ function Dot(runner, options) { }); runner.once(EVENT_RUN_END, function() { - println(); + self.println(); self.epilogue(); }); } diff --git a/lib/reporters/list.js b/lib/reporters/list.js index 7539426617..e3f35889cb 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -46,7 +46,7 @@ function List(runner, options) { var n = 0; runner.on(EVENT_RUN_BEGIN, function() { - println(); + self.println(); }); runner.on(EVENT_TEST_BEGIN, function(test) { @@ -55,7 +55,7 @@ function List(runner, options) { runner.on(EVENT_TEST_PENDING, function(test) { var fmt = color('checkmark', ' -') + color('pending', ' %s'); - println(fmt, test.fullTitle()); + self.println(fmt, test.fullTitle()); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -64,12 +64,12 @@ function List(runner, options) { color('pass', ' %s: ') + color(test.speed, '%dms'); cursor.CR(); - println(fmt, test.fullTitle(), test.duration); + self.println(fmt, test.fullTitle(), test.duration); }); runner.on(EVENT_TEST_FAIL, function(test) { cursor.CR(); - println(color('fail', ' %d) %s'), ++n, test.fullTitle()); + self.println(color('fail', ' %d) %s'), ++n, test.fullTitle()); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 6c2c09088d..9389113eb9 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -21,11 +21,6 @@ var cursor = Base.cursor; exports = module.exports = Progress; -/** - * Save log references to avoid tests interfering (see GH-3604). - */ -var println = console.log; - /** * General progress bar color. */ @@ -96,7 +91,7 @@ function Progress(runner, options) { // and the failures if any runner.once(EVENT_RUN_END, function() { cursor.show(); - println(); + self.println(); self.epilogue(); }); } diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index b6286738db..c082f631ac 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -51,24 +51,24 @@ function Spec(runner, options) { } runner.on(EVENT_RUN_BEGIN, function() { - println(); + self.println(); }); runner.on(EVENT_SUITE_BEGIN, function(suite) { ++indents; - println(color('suite', '%s%s'), indent(), suite.title); + self.println(color('suite', '%s%s'), indent(), suite.title); }); runner.on(EVENT_SUITE_END, function() { --indents; if (indents === 1) { - println(); + self.println(); } }); runner.on(EVENT_TEST_PENDING, function(test) { var fmt = indent() + color('pending', ' - %s'); - println(fmt, test.title); + self.println(fmt, test.title); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -78,19 +78,19 @@ function Spec(runner, options) { indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s'); - println(fmt, test.title); + self.println(fmt, test.title); } else { fmt = indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s') + color(test.speed, ' (%dms)'); - println(fmt, test.title, test.duration); + self.println(fmt, test.title, test.duration); } }); runner.on(EVENT_TEST_FAIL, function(test) { - println(indent() + color('fail', ' %d) %s'), ++n, test.title); + self.println(indent() + color('fail', ' %d) %s'), ++n, test.title); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js index 50cac91aab..7cd24bcb9e 100644 --- a/lib/reporters/xunit.js +++ b/lib/reporters/xunit.js @@ -27,11 +27,6 @@ var escape = utils.escape; */ var Date = global.Date; -/** - * Save log references to avoid tests interfering (see GH-3604). - */ -var println = console.log; - /** * Expose `XUnit`. */ @@ -147,7 +142,7 @@ XUnit.prototype.write = function(line) { } else if (typeof process === 'object' && process.stdout) { process.stdout.write(line + '\n'); } else { - println(line); + this.println(line); } }; From 76e220d079502f6560b4b8b2bb1636f449782256 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Wed, 13 Feb 2019 13:43:21 +0000 Subject: [PATCH 03/13] unit test change --- test/reporters/base.spec.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js index 739063f32a..fc6ea558a2 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -417,4 +417,20 @@ describe('Base reporter', function() { var errOut = stdout.join('\n').trim(); expect(errOut, 'to be', '1) test title:\n Error\n foo\n bar'); }); + + it('should let you stub out console.log without effecting reporters output', function() { + var logCached = console.log; + var logRes = []; + + console.log = function(line) { + logRes.push(line); + }; + var base = new Base({on: function() {}, stats: {duration: 0, passes: 3}}); + base.epilogue(); + + expect(stdout[1], 'to contain', '3 passing'); + expect(logRes, 'to have length', 0); + + console.log = logCached; + }); }); From 3778c5c91d15f9627412776f8610ce8d5892f13e Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Sat, 16 Feb 2019 19:03:44 +0000 Subject: [PATCH 04/13] use static property and fix test --- lib/reporters/base.js | 14 ++++++++------ lib/reporters/doc.js | 21 ++++++++++----------- lib/reporters/dot.js | 2 +- lib/reporters/list.js | 8 ++++---- lib/reporters/progress.js | 2 +- lib/reporters/spec.js | 14 +++++++------- lib/reporters/xunit.js | 2 +- test/reporters/base.spec.js | 18 ++++-------------- 8 files changed, 36 insertions(+), 45 deletions(-) diff --git a/lib/reporters/base.js b/lib/reporters/base.js index fc34e3e75f..34b1b9891e 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -313,7 +313,7 @@ Base.prototype.epilogue = function() { var stats = this.stats; var fmt; - this.println(); + Base.println(); // passes fmt = @@ -321,26 +321,26 @@ Base.prototype.epilogue = function() { color('green', ' %d passing') + color('light', ' (%s)'); - this.println(fmt, stats.passes || 0, milliseconds(stats.duration)); + Base.println(fmt, stats.passes || 0, milliseconds(stats.duration)); // pending if (stats.pending) { fmt = color('pending', ' ') + color('pending', ' %d pending'); - this.println(fmt, stats.pending); + Base.println(fmt, stats.pending); } // failures if (stats.failures) { fmt = color('fail', ' %d failing'); - this.println(fmt, stats.failures); + Base.println(fmt, stats.failures); Base.list(this.failures); - this.println(); + Base.println(); } - this.println(); + Base.println(); }; /** @@ -493,4 +493,6 @@ function sameType(a, b) { return objToString.call(a) === objToString.call(b); } +Base.println = println; + Base.abstract = true; diff --git a/lib/reporters/doc.js b/lib/reporters/doc.js index 117d74d43a..9bf8ce2b42 100644 --- a/lib/reporters/doc.js +++ b/lib/reporters/doc.js @@ -34,7 +34,6 @@ function Doc(runner, options) { Base.call(this, runner, options); var indents = 2; - var self = this; function indent() { return Array(indents).join(' '); @@ -45,41 +44,41 @@ function Doc(runner, options) { return; } ++indents; - self.println('%s
', indent()); + Base.println('%s
', indent()); ++indents; - self.println('%s

%s

', indent(), utils.escape(suite.title)); - self.println('%s
', indent()); + Base.println('%s

%s

', indent(), utils.escape(suite.title)); + Base.println('%s
', indent()); }); runner.on(EVENT_SUITE_END, function(suite) { if (suite.root) { return; } - self.println('%s
', indent()); + Base.println('%s
', indent()); --indents; - self.println('%s
', indent()); + Base.println('%s
', indent()); --indents; }); runner.on(EVENT_TEST_PASS, function(test) { - self.println('%s
%s
', indent(), utils.escape(test.title)); + Base.println('%s
%s
', indent(), utils.escape(test.title)); var code = utils.escape(utils.clean(test.body)); - self.println('%s
%s
', indent(), code); + Base.println('%s
%s
', indent(), code); }); runner.on(EVENT_TEST_FAIL, function(test, err) { - self.println( + Base.println( '%s
%s
', indent(), utils.escape(test.title) ); var code = utils.escape(utils.clean(test.body)); - self.println( + Base.println( '%s
%s
', indent(), code ); - self.println('%s
%s
', indent(), utils.escape(err)); + Base.println('%s
%s
', indent(), utils.escape(err)); }); } diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index bcb92697d2..eeceb4f533 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -73,7 +73,7 @@ function Dot(runner, options) { }); runner.once(EVENT_RUN_END, function() { - self.println(); + Base.println(); self.epilogue(); }); } diff --git a/lib/reporters/list.js b/lib/reporters/list.js index e3f35889cb..1c549e36ea 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -46,7 +46,7 @@ function List(runner, options) { var n = 0; runner.on(EVENT_RUN_BEGIN, function() { - self.println(); + Base.println(); }); runner.on(EVENT_TEST_BEGIN, function(test) { @@ -55,7 +55,7 @@ function List(runner, options) { runner.on(EVENT_TEST_PENDING, function(test) { var fmt = color('checkmark', ' -') + color('pending', ' %s'); - self.println(fmt, test.fullTitle()); + Base.println(fmt, test.fullTitle()); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -64,12 +64,12 @@ function List(runner, options) { color('pass', ' %s: ') + color(test.speed, '%dms'); cursor.CR(); - self.println(fmt, test.fullTitle(), test.duration); + Base.println(fmt, test.fullTitle(), test.duration); }); runner.on(EVENT_TEST_FAIL, function(test) { cursor.CR(); - self.println(color('fail', ' %d) %s'), ++n, test.fullTitle()); + Base.println(color('fail', ' %d) %s'), ++n, test.fullTitle()); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 9389113eb9..7cfefbc31f 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -91,7 +91,7 @@ function Progress(runner, options) { // and the failures if any runner.once(EVENT_RUN_END, function() { cursor.show(); - self.println(); + Base.println(); self.epilogue(); }); } diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index c082f631ac..a7114b5a55 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -51,24 +51,24 @@ function Spec(runner, options) { } runner.on(EVENT_RUN_BEGIN, function() { - self.println(); + Base.println(); }); runner.on(EVENT_SUITE_BEGIN, function(suite) { ++indents; - self.println(color('suite', '%s%s'), indent(), suite.title); + Base.println(color('suite', '%s%s'), indent(), suite.title); }); runner.on(EVENT_SUITE_END, function() { --indents; if (indents === 1) { - self.println(); + Base.println(); } }); runner.on(EVENT_TEST_PENDING, function(test) { var fmt = indent() + color('pending', ' - %s'); - self.println(fmt, test.title); + Base.println(fmt, test.title); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -78,19 +78,19 @@ function Spec(runner, options) { indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s'); - self.println(fmt, test.title); + Base.println(fmt, test.title); } else { fmt = indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s') + color(test.speed, ' (%dms)'); - self.println(fmt, test.title, test.duration); + Base.println(fmt, test.title, test.duration); } }); runner.on(EVENT_TEST_FAIL, function(test) { - self.println(indent() + color('fail', ' %d) %s'), ++n, test.title); + Base.println(indent() + color('fail', ' %d) %s'), ++n, test.title); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js index 7cd24bcb9e..9704c89cd1 100644 --- a/lib/reporters/xunit.js +++ b/lib/reporters/xunit.js @@ -142,7 +142,7 @@ XUnit.prototype.write = function(line) { } else if (typeof process === 'object' && process.stdout) { process.stdout.write(line + '\n'); } else { - this.println(line); + Base.println(line); } }; diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js index fc6ea558a2..3eb72e60e9 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -5,7 +5,7 @@ var chai = require('chai'); var sinon = require('sinon'); var helpers = require('./helpers'); var reporters = require('../../').reporters; - +var Base = reporters.Base; var AssertionError = assert.AssertionError; var Base = reporters.Base; var chaiExpect = chai.expect; @@ -419,18 +419,8 @@ describe('Base reporter', function() { }); it('should let you stub out console.log without effecting reporters output', function() { - var logCached = console.log; - var logRes = []; - - console.log = function(line) { - logRes.push(line); - }; - var base = new Base({on: function() {}, stats: {duration: 0, passes: 3}}); - base.epilogue(); - - expect(stdout[1], 'to contain', '3 passing'); - expect(logRes, 'to have length', 0); - - console.log = logCached; + sinon.stub(console, 'log'); + Base.list([]); + expect(console.log, 'was not called'); }); }); From 36022ed7f200c5fc73fa6510f2102bb4f572d7d7 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Sat, 16 Feb 2019 19:19:26 +0000 Subject: [PATCH 05/13] fix base test --- lib/reporters/base.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 34b1b9891e..c76fb3d807 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -197,7 +197,7 @@ var generateDiff = (exports.generateDiff = function(actual, expected) { * Error property */ exports.list = function(failures) { - println(); + Base.log(); failures.forEach(function(test, i) { // format var fmt = @@ -258,7 +258,7 @@ exports.list = function(failures) { testTitle += str; }); - println(fmt, i + 1, testTitle, msg, stack); + Base.log(fmt, i + 1, testTitle, msg, stack); }); }; From 47e8dc46fe6a998e4328cce001e3a026bbbe943e Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Sat, 2 Mar 2019 11:41:51 +0000 Subject: [PATCH 06/13] rename consoleLog --- lib/reporters/base.js | 20 ++++++++++---------- lib/reporters/doc.js | 24 ++++++++++++++---------- lib/reporters/dot.js | 2 +- lib/reporters/list.js | 8 ++++---- lib/reporters/progress.js | 2 +- lib/reporters/spec.js | 14 +++++++------- lib/reporters/xunit.js | 2 +- 7 files changed, 38 insertions(+), 34 deletions(-) diff --git a/lib/reporters/base.js b/lib/reporters/base.js index c76fb3d807..670a160fa8 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -30,7 +30,7 @@ var isatty = tty.isatty(1) && tty.isatty(2); /** * Save log references to avoid tests interfering (see GH-3604). */ -var println = console.log; +var consoleLog = console.log; /** * Enable coloring by default, except in the browser interface. @@ -197,7 +197,7 @@ var generateDiff = (exports.generateDiff = function(actual, expected) { * Error property */ exports.list = function(failures) { - Base.log(); + Base.consoleLog(); failures.forEach(function(test, i) { // format var fmt = @@ -258,7 +258,7 @@ exports.list = function(failures) { testTitle += str; }); - Base.log(fmt, i + 1, testTitle, msg, stack); + Base.consoleLog(fmt, i + 1, testTitle, msg, stack); }); }; @@ -313,7 +313,7 @@ Base.prototype.epilogue = function() { var stats = this.stats; var fmt; - Base.println(); + Base.consoleLog(); // passes fmt = @@ -321,26 +321,26 @@ Base.prototype.epilogue = function() { color('green', ' %d passing') + color('light', ' (%s)'); - Base.println(fmt, stats.passes || 0, milliseconds(stats.duration)); + Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration)); // pending if (stats.pending) { fmt = color('pending', ' ') + color('pending', ' %d pending'); - Base.println(fmt, stats.pending); + Base.consoleLog(fmt, stats.pending); } // failures if (stats.failures) { fmt = color('fail', ' %d failing'); - Base.println(fmt, stats.failures); + Base.consoleLog(fmt, stats.failures); Base.list(this.failures); - Base.println(); + Base.consoleLog(); } - Base.println(); + Base.consoleLog(); }; /** @@ -493,6 +493,6 @@ function sameType(a, b) { return objToString.call(a) === objToString.call(b); } -Base.println = println; +Base.consoleLog = consoleLog; Base.abstract = true; diff --git a/lib/reporters/doc.js b/lib/reporters/doc.js index 9bf8ce2b42..5a6af8fb42 100644 --- a/lib/reporters/doc.js +++ b/lib/reporters/doc.js @@ -44,41 +44,45 @@ function Doc(runner, options) { return; } ++indents; - Base.println('%s
', indent()); + Base.consoleLog('%s
', indent()); ++indents; - Base.println('%s

%s

', indent(), utils.escape(suite.title)); - Base.println('%s
', indent()); + Base.consoleLog('%s

%s

', indent(), utils.escape(suite.title)); + Base.consoleLog('%s
', indent()); }); runner.on(EVENT_SUITE_END, function(suite) { if (suite.root) { return; } - Base.println('%s
', indent()); + Base.consoleLog('%s
', indent()); --indents; - Base.println('%s
', indent()); + Base.consoleLog('%s
', indent()); --indents; }); runner.on(EVENT_TEST_PASS, function(test) { - Base.println('%s
%s
', indent(), utils.escape(test.title)); + Base.consoleLog('%s
%s
', indent(), utils.escape(test.title)); var code = utils.escape(utils.clean(test.body)); - Base.println('%s
%s
', indent(), code); + Base.consoleLog('%s
%s
', indent(), code); }); runner.on(EVENT_TEST_FAIL, function(test, err) { - Base.println( + Base.consoleLog( '%s
%s
', indent(), utils.escape(test.title) ); var code = utils.escape(utils.clean(test.body)); - Base.println( + Base.consoleLog( '%s
%s
', indent(), code ); - Base.println('%s
%s
', indent(), utils.escape(err)); + Base.consoleLog( + '%s
%s
', + indent(), + utils.escape(err) + ); }); } diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index eeceb4f533..c5ad347da8 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -73,7 +73,7 @@ function Dot(runner, options) { }); runner.once(EVENT_RUN_END, function() { - Base.println(); + Base.consoleLog(); self.epilogue(); }); } diff --git a/lib/reporters/list.js b/lib/reporters/list.js index 1c549e36ea..d9cdb9fb2b 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -46,7 +46,7 @@ function List(runner, options) { var n = 0; runner.on(EVENT_RUN_BEGIN, function() { - Base.println(); + Base.consoleLog(); }); runner.on(EVENT_TEST_BEGIN, function(test) { @@ -55,7 +55,7 @@ function List(runner, options) { runner.on(EVENT_TEST_PENDING, function(test) { var fmt = color('checkmark', ' -') + color('pending', ' %s'); - Base.println(fmt, test.fullTitle()); + Base.consoleLog(fmt, test.fullTitle()); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -64,12 +64,12 @@ function List(runner, options) { color('pass', ' %s: ') + color(test.speed, '%dms'); cursor.CR(); - Base.println(fmt, test.fullTitle(), test.duration); + Base.consoleLog(fmt, test.fullTitle(), test.duration); }); runner.on(EVENT_TEST_FAIL, function(test) { cursor.CR(); - Base.println(color('fail', ' %d) %s'), ++n, test.fullTitle()); + Base.consoleLog(color('fail', ' %d) %s'), ++n, test.fullTitle()); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 7cfefbc31f..47ffbcb224 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -91,7 +91,7 @@ function Progress(runner, options) { // and the failures if any runner.once(EVENT_RUN_END, function() { cursor.show(); - Base.println(); + Base.consoleLog(); self.epilogue(); }); } diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index a7114b5a55..ab09fa34b9 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -51,24 +51,24 @@ function Spec(runner, options) { } runner.on(EVENT_RUN_BEGIN, function() { - Base.println(); + Base.consoleLog(); }); runner.on(EVENT_SUITE_BEGIN, function(suite) { ++indents; - Base.println(color('suite', '%s%s'), indent(), suite.title); + Base.consoleLog(color('suite', '%s%s'), indent(), suite.title); }); runner.on(EVENT_SUITE_END, function() { --indents; if (indents === 1) { - Base.println(); + Base.consoleLog(); } }); runner.on(EVENT_TEST_PENDING, function(test) { var fmt = indent() + color('pending', ' - %s'); - Base.println(fmt, test.title); + Base.consoleLog(fmt, test.title); }); runner.on(EVENT_TEST_PASS, function(test) { @@ -78,19 +78,19 @@ function Spec(runner, options) { indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s'); - Base.println(fmt, test.title); + Base.consoleLog(fmt, test.title); } else { fmt = indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s') + color(test.speed, ' (%dms)'); - Base.println(fmt, test.title, test.duration); + Base.consoleLog(fmt, test.title, test.duration); } }); runner.on(EVENT_TEST_FAIL, function(test) { - Base.println(indent() + color('fail', ' %d) %s'), ++n, test.title); + Base.consoleLog(indent() + color('fail', ' %d) %s'), ++n, test.title); }); runner.once(EVENT_RUN_END, self.epilogue.bind(self)); diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js index 9704c89cd1..6c9c937be8 100644 --- a/lib/reporters/xunit.js +++ b/lib/reporters/xunit.js @@ -142,7 +142,7 @@ XUnit.prototype.write = function(line) { } else if (typeof process === 'object' && process.stdout) { process.stdout.write(line + '\n'); } else { - Base.println(line); + Base.consoleLog(line); } }; From fb3e8bf54e4191bc10fea4d5409fac0331afb781 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Sun, 17 Mar 2019 18:04:03 +0000 Subject: [PATCH 07/13] test include Base.consoleLog check --- lib/reporters/dot.js | 2 +- lib/reporters/progress.js | 2 +- test/reporters/base.spec.js | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index c5ad347da8..f7828d2a23 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -73,7 +73,7 @@ function Dot(runner, options) { }); runner.once(EVENT_RUN_END, function() { - Base.consoleLog(); + process.stdout.write('\n'); self.epilogue(); }); } diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 47ffbcb224..81226f2411 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -91,7 +91,7 @@ function Progress(runner, options) { // and the failures if any runner.once(EVENT_RUN_END, function() { cursor.show(); - Base.consoleLog(); + process.stdout.write('\n'); self.epilogue(); }); } diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js index 3eb72e60e9..e65a83b0f1 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -420,7 +420,13 @@ describe('Base reporter', function() { it('should let you stub out console.log without effecting reporters output', function() { sinon.stub(console, 'log'); + sinon.stub(Base, 'consoleLog'); Base.list([]); + + expect(Base.consoleLog, 'was called'); expect(console.log, 'was not called'); + + console.log.restore(); + Base.consoleLog.restore(); }); }); From 3bb6d62bd9a6527eeea67ed6865f09cc034ead41 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Sat, 6 Apr 2019 16:13:50 +0100 Subject: [PATCH 08/13] linter rule so reporters dont console.log --- .eslintrc.yml | 9 +++++++++ lib/reporters/landing.js | 2 +- lib/reporters/progress.js | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index f696b45ca8..a668139868 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -79,3 +79,12 @@ overrides: parserOptions: ecmaVersion: 6 sourceType: module + + - files: + - lib/reporters/*.js + rules: + no-restricted-syntax: + - error + # disallow Reporters using `console.log()` + - selector: 'CallExpression[callee.object.name=console][callee.property.name=log]' + message: &GH-3604 See https://github.com/mochajs/mocha/issues/3604 diff --git a/lib/reporters/landing.js b/lib/reporters/landing.js index b124c7789c..a6af946c42 100644 --- a/lib/reporters/landing.js +++ b/lib/reporters/landing.js @@ -95,7 +95,7 @@ function Landing(runner, options) { runner.once(EVENT_RUN_END, function() { cursor.show(); - console.log(); + process.stdout.write('\n'); self.epilogue(); }); } diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 81226f2411..0211122a9d 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -58,7 +58,7 @@ function Progress(runner, options) { // tests started runner.on(EVENT_RUN_BEGIN, function() { - console.log(); + process.stdout.write('\n'); cursor.hide(); }); From 5376b80f7521a5c94767989f4c5b0656d681f673 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Wed, 10 Apr 2019 22:42:35 +0100 Subject: [PATCH 09/13] use hooks for stubs with base test --- test/reporters/base.spec.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js index e65a83b0f1..098745eea7 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -418,15 +418,24 @@ describe('Base reporter', function() { expect(errOut, 'to be', '1) test title:\n Error\n foo\n bar'); }); - it('should let you stub out console.log without effecting reporters output', function() { - sinon.stub(console, 'log'); - sinon.stub(Base, 'consoleLog'); - Base.list([]); + describe('when reporter output immune to user test changes', function() { + var sandbox; - expect(Base.consoleLog, 'was called'); - expect(console.log, 'was not called'); + beforeEach(function() { + sandbox = sinon.createSandbox(); + sandbox.stub(console, 'log'); + sandbox.stub(Base, 'consoleLog').callThrough(); + }); + + it('should let you stub out console.log without effecting reporters output', function() { + Base.list([]); - console.log.restore(); - Base.consoleLog.restore(); + expect(Base.consoleLog, 'was called'); + expect(console.log, 'was not called'); + }); + + afterEach(function() { + sandbox.restore(); + }); }); }); From 19cc3f5b761c25923bedac424d72f309b61dc1b3 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Sat, 13 Apr 2019 18:10:41 +0100 Subject: [PATCH 10/13] restore stub dont callThrough --- test/reporters/base.spec.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js index 098745eea7..7019cde1c4 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -420,17 +420,19 @@ describe('Base reporter', function() { describe('when reporter output immune to user test changes', function() { var sandbox; + var baseConsoleLog; beforeEach(function() { sandbox = sinon.createSandbox(); sandbox.stub(console, 'log'); - sandbox.stub(Base, 'consoleLog').callThrough(); + baseConsoleLog = sandbox.stub(Base, 'consoleLog'); }); it('should let you stub out console.log without effecting reporters output', function() { Base.list([]); + baseConsoleLog.restore(); - expect(Base.consoleLog, 'was called'); + expect(baseConsoleLog, 'was called'); expect(console.log, 'was not called'); }); From 97b3a9db0d63b028116db6ad4f8f7b220dcd3d47 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Mon, 17 Jun 2019 19:05:18 +0100 Subject: [PATCH 11/13] fix test + rebase --- test/reporters/xunit.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/reporters/xunit.spec.js b/test/reporters/xunit.spec.js index 26fce4a1c4..2d05312fae 100644 --- a/test/reporters/xunit.spec.js +++ b/test/reporters/xunit.spec.js @@ -277,14 +277,14 @@ describe('XUnit reporter', function() { }); describe('when output directed to console', function() { - it("should call 'console.log' with line", function() { + it("should call 'Base.consoleLog' with line", function() { // :TODO: XUnit needs a trivially testable means to force console.log() var realProcess = process; process = false; // eslint-disable-line no-native-reassign, no-global-assign var xunit = new XUnit(runner); var fakeThis = {fileStream: false}; - var consoleLogStub = sinon.stub(console, 'log'); + var consoleLogStub = sinon.stub(Base, 'consoleLog'); xunit.write.call(fakeThis, expectedLine); consoleLogStub.restore(); From bc82689629374e2e6e9d207a0608965b07e2d533 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Mon, 17 Jun 2019 19:09:14 +0100 Subject: [PATCH 12/13] fix faulty rebase. Removed printLn --- lib/reporters/dot.js | 5 ----- lib/reporters/list.js | 5 ----- lib/reporters/spec.js | 5 ----- 3 files changed, 15 deletions(-) diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index f7828d2a23..3913f0c679 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -21,11 +21,6 @@ var EVENT_RUN_END = constants.EVENT_RUN_END; exports = module.exports = Dot; -/** - * Save log references to avoid tests interfering (see GH-3604). - */ -var println = console.log; - /** * Constructs a new `Dot` reporter instance. * diff --git a/lib/reporters/list.js b/lib/reporters/list.js index d9cdb9fb2b..c7ff8c4ea8 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -24,11 +24,6 @@ var cursor = Base.cursor; exports = module.exports = List; -/** - * Save log references to avoid tests interfering (see GH-3604). - */ -var println = console.log; - /** * Constructs a new `List` reporter instance. * diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index ab09fa34b9..e51ed80ac4 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -24,11 +24,6 @@ var color = Base.color; exports = module.exports = Spec; -/** - * Save log references to avoid tests interfering (see GH-3604). - */ -var println = console.log; - /** * Constructs a new `Spec` reporter instance. * From 62569cc4b1c71731ec0d4a40d8888271d50ec898 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Mon, 17 Jun 2019 19:28:35 +0100 Subject: [PATCH 13/13] remove superfluous base --- test/reporters/base.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/reporters/base.spec.js b/test/reporters/base.spec.js index 7019cde1c4..80957c39aa 100644 --- a/test/reporters/base.spec.js +++ b/test/reporters/base.spec.js @@ -5,7 +5,6 @@ var chai = require('chai'); var sinon = require('sinon'); var helpers = require('./helpers'); var reporters = require('../../').reporters; -var Base = reporters.Base; var AssertionError = assert.AssertionError; var Base = reporters.Base; var chaiExpect = chai.expect;