From 4810446185a566b58805fb9b0cfd7a51fd3d6cbf Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Fri, 27 Jan 2017 23:26:33 +0000 Subject: [PATCH 1/5] Increase tests coverage for doc reporter --- test/reporters/doc.spec.js | 148 +++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 test/reporters/doc.spec.js diff --git a/test/reporters/doc.spec.js b/test/reporters/doc.spec.js new file mode 100644 index 0000000000..29a86e21e1 --- /dev/null +++ b/test/reporters/doc.spec.js @@ -0,0 +1,148 @@ +'use strict'; + +var reporters = require('../../').reporters; +var Doc = reporters.Doc; + +describe('Doc reporter', function () { + var stdout; + var stdoutWrite; + var runner = {}; + beforeEach(function () { + stdout = []; + stdoutWrite = process.stdout.write; + process.stdout.write = function (string) { + stdout.push(string); + }; + }); + + describe('on suite', function() { + describe('if suite root does not exist', function() { + var expectedTitle = 'expectedTitle'; + var suite = { + root: false, + title: expectedTitle + } + it('should log html with expected header', function () { + runner.on = function (event, callback) { + if (event === 'suite') { + callback(suite); + } + } + var doc = new Doc(runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
\n', + '

' + expectedTitle + '

\n', + '
\n' + ]; + stdout.should.deepEqual(expectedArray) + }); + }); + describe('if suite root does exist', function() { + var suite = { + root: true + } + it('should not log any html', function () { + runner.on = function (event, callback) { + if (event === 'suite') { + callback(suite); + } + } + var doc = new Doc(runner); + process.stdout.write = stdoutWrite; + stdout.should.be.empty(); + }); + }); + }); + + describe('on suite end', function() { + describe('if suite root does not exist', function() { + var expectedTitle = 'expectedTitle'; + var suite = { + root: false, + title: expectedTitle + } + it('should log expected html', function () { + runner.on = function (event, callback) { + if (event === 'suite end') { + callback(suite); + } + } + var doc = new Doc(runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
\n', '
\n' + ]; + stdout.should.deepEqual(expectedArray) + }); + }); + describe('if suite root does exist', function() { + var suite = { + root: true + } + it('should not log any html', function () { + runner.on = function (event, callback) { + if (event === 'suite end') { + callback(suite); + } + } + var doc = new Doc(runner); + process.stdout.write = stdoutWrite; + stdout.should.be.empty(); + }); + }); + }); + + describe('on pass', function() { + var expectedTitle = 'some tite'; + var expectedBody = 'some body'; + var test = { + title: expectedTitle, + body: expectedBody, + slow: function () { + return ''; + } + } + it('should log expected html', function () { + runner.on = function (event, callback) { + if (event === 'pass') { + callback(test); + } + } + var doc = new Doc(runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedTitle + '
\n', + '
' + expectedBody + '
\n' + ]; + stdout.should.deepEqual(expectedArray) + }); + }); + + describe('on fail', function() { + var expectedTitle = 'some tite'; + var expectedBody = 'some body'; + var test = { + title: expectedTitle, + body: expectedBody, + slow: function () { + return ''; + } + } + it('should log expected html', function () { + runner.on = function (event, callback) { + if (event === 'fail') { + callback(test); + } + } + var doc = new Doc(runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedTitle + '
\n', + '
' + expectedBody + '
\n', + '
undefined
\n' + ]; + stdout.should.deepEqual(expectedArray) + }); + }); +}); From b8a62376c97e86ed4e81e9bd7682926ea7587a76 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Fri, 27 Jan 2017 23:39:02 +0000 Subject: [PATCH 2/5] updated spec message --- test/reporters/doc.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/reporters/doc.spec.js b/test/reporters/doc.spec.js index 29a86e21e1..febce1ed39 100644 --- a/test/reporters/doc.spec.js +++ b/test/reporters/doc.spec.js @@ -22,7 +22,7 @@ describe('Doc reporter', function () { root: false, title: expectedTitle } - it('should log html with expected header', function () { + it('should log html with expected title', function () { runner.on = function (event, callback) { if (event === 'suite') { callback(suite); @@ -103,7 +103,7 @@ describe('Doc reporter', function () { return ''; } } - it('should log expected html', function () { + it('should log html with expected title and body', function () { runner.on = function (event, callback) { if (event === 'pass') { callback(test); @@ -129,7 +129,7 @@ describe('Doc reporter', function () { return ''; } } - it('should log expected html', function () { + it('should log html with expected title and body', function () { runner.on = function (event, callback) { if (event === 'fail') { callback(test); From dc0132390478b5ba8d4b6b15d76579c79ebf4f96 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Fri, 27 Jan 2017 23:54:06 +0000 Subject: [PATCH 3/5] fix linting errors --- test/reporters/doc.spec.js | 70 ++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/test/reporters/doc.spec.js b/test/reporters/doc.spec.js index febce1ed39..f62c833183 100644 --- a/test/reporters/doc.spec.js +++ b/test/reporters/doc.spec.js @@ -15,85 +15,83 @@ describe('Doc reporter', function () { }; }); - describe('on suite', function() { - describe('if suite root does not exist', function() { + describe('on suite', function () { + describe('if suite root does not exist', function () { var expectedTitle = 'expectedTitle'; var suite = { root: false, title: expectedTitle - } + }; it('should log html with expected title', function () { runner.on = function (event, callback) { if (event === 'suite') { callback(suite); } - } - var doc = new Doc(runner); + }; + Doc(runner); process.stdout.write = stdoutWrite; var expectedArray = [ '
\n', '

' + expectedTitle + '

\n', '
\n' ]; - stdout.should.deepEqual(expectedArray) + stdout.should.deepEqual(expectedArray); }); }); - describe('if suite root does exist', function() { + describe('if suite root does exist', function () { var suite = { root: true - } + }; it('should not log any html', function () { runner.on = function (event, callback) { if (event === 'suite') { callback(suite); } - } - var doc = new Doc(runner); + }; + Doc(runner); process.stdout.write = stdoutWrite; stdout.should.be.empty(); }); }); }); - describe('on suite end', function() { - describe('if suite root does not exist', function() { - var expectedTitle = 'expectedTitle'; + describe('on suite end', function () { + describe('if suite root does not exist', function () { var suite = { - root: false, - title: expectedTitle - } + root: false + }; it('should log expected html', function () { runner.on = function (event, callback) { if (event === 'suite end') { callback(suite); } - } - var doc = new Doc(runner); + }; + Doc(runner); process.stdout.write = stdoutWrite; var expectedArray = [ '
\n', '
\n' ]; - stdout.should.deepEqual(expectedArray) + stdout.should.deepEqual(expectedArray); }); }); - describe('if suite root does exist', function() { + describe('if suite root does exist', function () { var suite = { root: true - } + }; it('should not log any html', function () { runner.on = function (event, callback) { if (event === 'suite end') { callback(suite); } - } - var doc = new Doc(runner); + }; + Doc(runner); process.stdout.write = stdoutWrite; stdout.should.be.empty(); }); }); }); - describe('on pass', function() { + describe('on pass', function () { var expectedTitle = 'some tite'; var expectedBody = 'some body'; var test = { @@ -102,24 +100,24 @@ describe('Doc reporter', function () { slow: function () { return ''; } - } + }; it('should log html with expected title and body', function () { runner.on = function (event, callback) { if (event === 'pass') { callback(test); } - } - var doc = new Doc(runner); + }; + Doc(runner); process.stdout.write = stdoutWrite; var expectedArray = [ '
' + expectedTitle + '
\n', '
' + expectedBody + '
\n' ]; - stdout.should.deepEqual(expectedArray) + stdout.should.deepEqual(expectedArray); }); }); - describe('on fail', function() { + describe('on fail', function () { var expectedTitle = 'some tite'; var expectedBody = 'some body'; var test = { @@ -128,21 +126,21 @@ describe('Doc reporter', function () { slow: function () { return ''; } - } + }; it('should log html with expected title and body', function () { runner.on = function (event, callback) { if (event === 'fail') { callback(test); } - } - var doc = new Doc(runner); + }; + Doc(runner); process.stdout.write = stdoutWrite; var expectedArray = [ - '
' + expectedTitle + '
\n', - '
' + expectedBody + '
\n', - '
undefined
\n' + '
' + expectedTitle + '
\n', + '
' + expectedBody + '
\n', + '
undefined
\n' ]; - stdout.should.deepEqual(expectedArray) + stdout.should.deepEqual(expectedArray); }); }); }); From 3d154c0f437c6b97fbc7c58dda1d47779b832c72 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Fri, 27 Jan 2017 23:57:54 +0000 Subject: [PATCH 4/5] replace new with call --- test/reporters/doc.spec.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/reporters/doc.spec.js b/test/reporters/doc.spec.js index f62c833183..61a5016e8f 100644 --- a/test/reporters/doc.spec.js +++ b/test/reporters/doc.spec.js @@ -28,7 +28,7 @@ describe('Doc reporter', function () { callback(suite); } }; - Doc(runner); + Doc.call(this, runner); process.stdout.write = stdoutWrite; var expectedArray = [ '
\n', @@ -48,7 +48,7 @@ describe('Doc reporter', function () { callback(suite); } }; - Doc(runner); + Doc.call(this, runner); process.stdout.write = stdoutWrite; stdout.should.be.empty(); }); @@ -66,7 +66,7 @@ describe('Doc reporter', function () { callback(suite); } }; - Doc(runner); + Doc.call(this, runner); process.stdout.write = stdoutWrite; var expectedArray = [ ' \n', '
\n' @@ -84,7 +84,7 @@ describe('Doc reporter', function () { callback(suite); } }; - Doc(runner); + Doc.call(this, runner); process.stdout.write = stdoutWrite; stdout.should.be.empty(); }); @@ -107,7 +107,7 @@ describe('Doc reporter', function () { callback(test); } }; - Doc(runner); + Doc.call(this, runner); process.stdout.write = stdoutWrite; var expectedArray = [ '
' + expectedTitle + '
\n', @@ -133,7 +133,7 @@ describe('Doc reporter', function () { callback(test); } }; - Doc(runner); + Doc.call(this, runner); process.stdout.write = stdoutWrite; var expectedArray = [ '
' + expectedTitle + '
\n', From f17245f7af0b0fb3e03dbffd1a2754b3e721c6e9 Mon Sep 17 00:00:00 2001 From: Craig Taub Date: Sat, 28 Jan 2017 11:04:59 +0000 Subject: [PATCH 5/5] add tests for indents and escaping --- test/reporters/doc.spec.js | 79 +++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/test/reporters/doc.spec.js b/test/reporters/doc.spec.js index 61a5016e8f..2e0d1e029e 100644 --- a/test/reporters/doc.spec.js +++ b/test/reporters/doc.spec.js @@ -18,11 +18,32 @@ describe('Doc reporter', function () { describe('on suite', function () { describe('if suite root does not exist', function () { var expectedTitle = 'expectedTitle'; + var unescapedTitle = '
' + expectedTitle + '
'; var suite = { root: false, title: expectedTitle }; - it('should log html with expected title', function () { + it('should log html with indents and expected title', function () { + runner.on = function (event, callback) { + if (event === 'suite') { + callback(suite); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
\n', + '

' + expectedTitle + '

\n', + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + it('should escape title where necessary', function () { + var suite = { + root: false, + title: unescapedTitle + }; + expectedTitle = '<div>' + expectedTitle + '</div>'; runner.on = function (event, callback) { if (event === 'suite') { callback(suite); @@ -60,7 +81,7 @@ describe('Doc reporter', function () { var suite = { root: false }; - it('should log expected html', function () { + it('should log expected html with indents', function () { runner.on = function (event, callback) { if (event === 'suite end') { callback(suite); @@ -101,7 +122,7 @@ describe('Doc reporter', function () { return ''; } }; - it('should log html with expected title and body', function () { + it('should log html with indents and expected title and body', function () { runner.on = function (event, callback) { if (event === 'pass') { callback(test); @@ -115,11 +136,33 @@ describe('Doc reporter', function () { ]; stdout.should.deepEqual(expectedArray); }); + it('should escape title and body where necessary', function () { + var unescapedTitle = '
' + expectedTitle + '
'; + var unescapedBody = '
' + expectedBody + '
'; + test.title = unescapedTitle; + test.body = unescapedBody; + + var expectedEscapedTitle = '<div>' + expectedTitle + '</div>'; + var expectedEscapedBody = '<div>' + expectedBody + '</div>'; + runner.on = function (event, callback) { + if (event === 'pass') { + callback(test); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedEscapedTitle + '
\n', + '
' + expectedEscapedBody + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); }); describe('on fail', function () { var expectedTitle = 'some tite'; var expectedBody = 'some body'; + var expectedError = 'some error'; var test = { title: expectedTitle, body: expectedBody, @@ -127,10 +170,10 @@ describe('Doc reporter', function () { return ''; } }; - it('should log html with expected title and body', function () { + it('should log html with indents and expected title, body and error', function () { runner.on = function (event, callback) { if (event === 'fail') { - callback(test); + callback(test, expectedError); } }; Doc.call(this, runner); @@ -138,7 +181,31 @@ describe('Doc reporter', function () { var expectedArray = [ '
' + expectedTitle + '
\n', '
' + expectedBody + '
\n', - '
undefined
\n' + '
' + expectedError + '
\n' + ]; + stdout.should.deepEqual(expectedArray); + }); + it('should escape title, body and error where necessary', function () { + var unescapedTitle = '
' + expectedTitle + '
'; + var unescapedBody = '
' + expectedBody + '
'; + var unescapedError = '
' + expectedError + '
'; + test.title = unescapedTitle; + test.body = unescapedBody; + + var expectedEscapedTitle = '<div>' + expectedTitle + '</div>'; + var expectedEscapedBody = '<div>' + expectedBody + '</div>'; + var expectedEscapedError = '<div>' + expectedError + '</div>'; + runner.on = function (event, callback) { + if (event === 'fail') { + callback(test, unescapedError); + } + }; + Doc.call(this, runner); + process.stdout.write = stdoutWrite; + var expectedArray = [ + '
' + expectedEscapedTitle + '
\n', + '
' + expectedEscapedBody + '
\n', + '
' + expectedEscapedError + '
\n' ]; stdout.should.deepEqual(expectedArray); });