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. diff --git a/src/index.js b/src/index.js index 3432c27..07aaa93 100644 --- a/src/index.js +++ b/src/index.js @@ -51,8 +51,13 @@ module.exports = { }); }, - postTest: function() { + 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; @@ -66,6 +71,12 @@ module.exports = { return; } + result.forEach(resultLine => { + if (config.failOnSevere && resultLine.level.name.toLowerCase() === LEVELS.severe.name) { + addFailure(`Test failed due to ${LEVELS.severe.name} level message`); + } + }); + printHeader.call(config); _(result) diff --git a/test/index.js b/test/index.js index b91820a..ee0693f 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.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', () => {