From 519e84381996b4a149906112689c2d5eafd6e25d Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 16 Aug 2017 14:30:00 -0700 Subject: [PATCH 1/8] Add ability to cause the test to fail --- src/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/index.js b/src/index.js index 3432c27..19878ed 100644 --- a/src/index.js +++ b/src/index.js @@ -52,6 +52,7 @@ module.exports = { }, postTest: function() { + let addFailure = this.addFailure; let config = this.config; if (!this.enabled) { @@ -66,6 +67,10 @@ module.exports = { return; } + if (config.failOnMessage) { + addFailure('Unexpected console messages were logged.'); + } + printHeader.call(config); _(result) From 5e32c7386d8d604b633bf47f77125d79dd79aeae Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 16 May 2018 23:12:48 -0700 Subject: [PATCH 2/8] spacing --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index b91820a..1d825b1 100644 --- a/test/index.js +++ b/test/index.js @@ -8,7 +8,7 @@ let expect = chai.expect; chai.use(sinonChai); describe('protractor-console', () => { - let reporter, printerSpy, headerSpy; + let reporter, printerSpy, headerSpy; beforeEach(() => { let browser = global.browser = {}; From 53e2f8d076c1c8c228586b8d7b3a232da526f265 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 16 May 2018 23:35:55 -0700 Subject: [PATCH 3/8] change flag to failOnSevere --- src/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 19878ed..03f764c 100644 --- a/src/index.js +++ b/src/index.js @@ -67,9 +67,11 @@ module.exports = { return; } - if (config.failOnMessage) { - addFailure('Unexpected console messages were logged.'); - } + result.forEach(resultLine => { + if (config.failOnSevere && resultLine.level.name === LEVELS.severe.name) { + config.addFailure(`Test failed due to ${LEVELS.severe.name} level message`); + } + }); printHeader.call(config); From 480a62c10d8d94165b22c9d7e00faf18c5a3550a Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 16 May 2018 23:44:22 -0700 Subject: [PATCH 4/8] add tests for failOnSevere --- src/index.js | 5 ++--- test/index.js | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 03f764c..5302a23 100644 --- a/src/index.js +++ b/src/index.js @@ -51,8 +51,7 @@ module.exports = { }); }, - postTest: function() { - let addFailure = this.addFailure; + postTest: function () { let config = this.config; if (!this.enabled) { @@ -68,7 +67,7 @@ module.exports = { } result.forEach(resultLine => { - if (config.failOnSevere && resultLine.level.name === LEVELS.severe.name) { + if (config.failOnSevere && resultLine.level.name.toLowerCase() === LEVELS.severe.name) { config.addFailure(`Test failed due to ${LEVELS.severe.name} level message`); } }); diff --git a/test/index.js b/test/index.js index 1d825b1..814918f 100644 --- a/test/index.js +++ b/test/index.js @@ -8,7 +8,7 @@ let expect = chai.expect; chai.use(sinonChai); describe('protractor-console', () => { - let reporter, printerSpy, headerSpy; + let reporter, printerSpy, headerSpy, addFailureSpy; beforeEach(() => { let browser = global.browser = {}; @@ -32,11 +32,44 @@ describe('protractor-console', () => { headerSpy = sinon.spy(); reporter.config.headerPrinter = headerSpy; + + addFailureSpy = sinon.spy(); + reporter.config.addFailure = addFailureSpy; }); afterEach(() => { printerSpy.reset(); headerSpy.reset(); + addFailureSpy.reset(); + }); + + it('should fail tests when severe error and failOnSevere==true', () => { + reporter.config.failOnSevere = true; + expect(addFailureSpy).to.have.callCount(0); + + return reporter.postTest() + .then(() => { + expect(addFailureSpy).to.have.callCount(1); + }); + }); + + it('should not fail tests when severe error and failOnSevere==false', () => { + reporter.config.failOnSevere = false; + expect(addFailureSpy).to.have.callCount(0); + + return reporter.postTest() + .then(() => { + expect(addFailureSpy).to.have.callCount(0); + }); + }); + + it('should not fail tests when severe error and failOnSevere undefined', () => { + expect(addFailureSpy).to.have.callCount(0); + + return reporter.postTest() + .then(() => { + expect(addFailureSpy).to.have.callCount(0); + }); }); it('should filter by log level', () => { From 64daa14c70884c41f6baf8df26f7dae5ede0e1ff Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 16 May 2018 23:50:08 -0700 Subject: [PATCH 5/8] add failOnSevere docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8190f1e..424b360 100644 --- a/README.md +++ b/README.md @@ -26,4 +26,4 @@ exports.config = { ### Configuration * `logLevels`: Inclusive `Array` filter for which log levels to show. Can be any of `'debug'`, `'info'`, `'warning'` and `'severe'`. Defaults to `['severe', 'warning']`. - +* `failOnSevere`: `Boolean` for failing the tests when `'severe'` level errors detected. Disabled by default. From 82b1dd667c54be606fd3036ca1e0435424a6ee97 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Thu, 17 May 2018 00:16:58 -0700 Subject: [PATCH 6/8] we arent calling the right function, actually? --- test/index.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/test/index.js b/test/index.js index 814918f..968861b 100644 --- a/test/index.js +++ b/test/index.js @@ -32,44 +32,65 @@ describe('protractor-console', () => { headerSpy = sinon.spy(); reporter.config.headerPrinter = headerSpy; - - addFailureSpy = sinon.spy(); - reporter.config.addFailure = addFailureSpy; }); afterEach(() => { printerSpy.reset(); headerSpy.reset(); - addFailureSpy.reset(); + }); + + it('runs without addFailureSpy', () => { + reporter.config.failOnSevere = true; + + return reporter.postTest() + .then(() => { + expect(headerSpy).to.have.callCount(0); + return reporter.postTest.call(reporter); + }) + .then(() => { + expect(headerSpy).to.have.callCount(1); + }); }); it('should fail tests when severe error and failOnSevere==true', () => { reporter.config.failOnSevere = true; + addFailureSpy = sinon.spy(); + reporter.config.addFailure = addFailureSpy; expect(addFailureSpy).to.have.callCount(0); return reporter.postTest() .then(() => { expect(addFailureSpy).to.have.callCount(1); }); + + addFailureSpy.reset(); }); it('should not fail tests when severe error and failOnSevere==false', () => { reporter.config.failOnSevere = false; + addFailureSpy = sinon.spy(); + reporter.config.addFailure = addFailureSpy; expect(addFailureSpy).to.have.callCount(0); return reporter.postTest() .then(() => { expect(addFailureSpy).to.have.callCount(0); }); + + addFailureSpy.reset(); }); it('should not fail tests when severe error and failOnSevere undefined', () => { + addFailureSpy = sinon.spy(); + reporter.config.addFailure = addFailureSpy; expect(addFailureSpy).to.have.callCount(0); return reporter.postTest() .then(() => { expect(addFailureSpy).to.have.callCount(0); }); + + addFailureSpy.reset(); }); it('should filter by log level', () => { From 4f80ca6e86a42ee1e490a6ed28c73682d0420e1e Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Thu, 17 May 2018 00:20:10 -0700 Subject: [PATCH 7/8] more flexible assertion --- test/index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/index.js b/test/index.js index 968861b..fd337db 100644 --- a/test/index.js +++ b/test/index.js @@ -44,11 +44,7 @@ describe('protractor-console', () => { return reporter.postTest() .then(() => { - expect(headerSpy).to.have.callCount(0); - return reporter.postTest.call(reporter); - }) - .then(() => { - expect(headerSpy).to.have.callCount(1); + expect(true).toBeTruthy(); }); }); From 3d7a8ac62d9f0271514573b2e748ca8aefefdcdf Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Thu, 17 May 2018 00:38:38 -0700 Subject: [PATCH 8/8] figured out where addFailure comes from! --- src/index.js | 7 ++++++- test/index.js | 25 ++++--------------------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/index.js b/src/index.js index 5302a23..07aaa93 100644 --- a/src/index.js +++ b/src/index.js @@ -52,7 +52,12 @@ module.exports = { }, postTest: function () { + // this == ProtractorPlugin + // from: https://github.com/angular/protractor/blob/c6703a5ea8ce7a837193ecf478c2096d8c6e99e9/lib/plugins.ts#L25 let config = this.config; + // addFailure == ProtractorPlugin.addFailure + // https://github.com/angular/protractor/blob/c6703a5ea8ce7a837193ecf478c2096d8c6e99e9/lib/plugins.ts#L243 + let addFailure = this.addFailure; if (!this.enabled) { return; @@ -68,7 +73,7 @@ module.exports = { result.forEach(resultLine => { if (config.failOnSevere && resultLine.level.name.toLowerCase() === LEVELS.severe.name) { - config.addFailure(`Test failed due to ${LEVELS.severe.name} level message`); + addFailure(`Test failed due to ${LEVELS.severe.name} level message`); } }); diff --git a/test/index.js b/test/index.js index fd337db..ee0693f 100644 --- a/test/index.js +++ b/test/index.js @@ -32,61 +32,44 @@ describe('protractor-console', () => { headerSpy = sinon.spy(); reporter.config.headerPrinter = headerSpy; + + addFailureSpy = sinon.spy(); + reporter.addFailure = addFailureSpy; }); afterEach(() => { printerSpy.reset(); headerSpy.reset(); - }); - - it('runs without addFailureSpy', () => { - reporter.config.failOnSevere = true; - - return reporter.postTest() - .then(() => { - expect(true).toBeTruthy(); - }); + addFailureSpy.reset(); }); it('should fail tests when severe error and failOnSevere==true', () => { reporter.config.failOnSevere = true; - addFailureSpy = sinon.spy(); - reporter.config.addFailure = addFailureSpy; expect(addFailureSpy).to.have.callCount(0); return reporter.postTest() .then(() => { expect(addFailureSpy).to.have.callCount(1); }); - - addFailureSpy.reset(); }); it('should not fail tests when severe error and failOnSevere==false', () => { reporter.config.failOnSevere = false; - addFailureSpy = sinon.spy(); - reporter.config.addFailure = addFailureSpy; expect(addFailureSpy).to.have.callCount(0); return reporter.postTest() .then(() => { expect(addFailureSpy).to.have.callCount(0); }); - - addFailureSpy.reset(); }); it('should not fail tests when severe error and failOnSevere undefined', () => { - addFailureSpy = sinon.spy(); - reporter.config.addFailure = addFailureSpy; expect(addFailureSpy).to.have.callCount(0); return reporter.postTest() .then(() => { expect(addFailureSpy).to.have.callCount(0); }); - - addFailureSpy.reset(); }); it('should filter by log level', () => {