From 8bc1c1b6a593bdc6a847760eaed8f2a332249635 Mon Sep 17 00:00:00 2001 From: Honza Javorek Date: Wed, 16 Jan 2019 19:02:37 +0100 Subject: [PATCH] test: update tests to reflect the logging split --- .../integration/cli/configuration-cli-test.js | 24 ++-- test/integration/cli/hookfiles-cli-test.js | 8 +- test/integration/dredd-test.js | 105 +++++++----------- test/integration/helpers.js | 21 +++- test/unit/CLI-test.js | 25 ++--- test/unit/hooksLog-test.js | 37 +++--- test/unit/reporters/ApiaryReporter-test.js | 22 ++-- test/unit/reporters/CLIReporter-test.js | 64 ++++++----- test/unit/reporters/DotReporter-test.js | 13 ++- test/unit/reporters/HTMLReporter-test.js | 24 ++-- test/unit/reporters/MarkdownReporter-test.js | 24 ++-- test/unit/reporters/NyanReporter-test.js | 24 ++-- test/unit/reporters/XUnitReporter-test.js | 26 +++-- 13 files changed, 209 insertions(+), 208 deletions(-) diff --git a/test/integration/cli/configuration-cli-test.js b/test/integration/cli/configuration-cli-test.js index 9c21d69f3..760028895 100644 --- a/test/integration/cli/configuration-cli-test.js +++ b/test/integration/cli/configuration-cli-test.js @@ -54,25 +54,21 @@ function execCommand(custom = {}, cb) { describe('CLI class Integration', () => { before(() => { ['warn', 'error'].forEach((method) => { - sinon.stub(loggerStub, method).callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; }); + sinon + .stub(loggerStub, method) + .callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; }); }); - [ - 'log', 'info', 'silly', 'verbose', 'test', - 'hook', 'complete', 'pass', 'skip', 'debug', - 'fail', 'request', 'expected', 'actual', - ].forEach((method) => { - sinon.stub(loggerStub, method).callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; }); + ['log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => { + sinon + .stub(loggerStub, method) + .callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; }); }); }); after(() => { - ['warn', 'error'].forEach((method) => { loggerStub[method].restore(); }); - - [ - 'log', 'info', 'silly', 'verbose', 'test', - 'hook', 'complete', 'pass', 'skip', 'debug', - 'fail', 'request', 'expected', 'actual', - ].forEach((method) => { loggerStub[method].restore(); }); + ['warn', 'error', 'log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => { + loggerStub[method].restore(); + }); }); describe('When using configuration file', () => { diff --git a/test/integration/cli/hookfiles-cli-test.js b/test/integration/cli/hookfiles-cli-test.js index 2652a5749..1de2d51c3 100644 --- a/test/integration/cli/hookfiles-cli-test.js +++ b/test/integration/cli/hookfiles-cli-test.js @@ -533,7 +533,7 @@ describe('CLI', () => { }); }); - describe('when setting the log output level with -l', () => { + describe('when setting the log output level with --level', () => { let runtimeInfo; before((done) => { @@ -543,7 +543,8 @@ describe('CLI', () => { const args = [ './test/fixtures/single-get.apib', `http://127.0.0.1:${DEFAULT_SERVER_PORT}`, - '-l=error', + '--level=error', + '--color=false', ]; runCLIWithServer(args, app, (err, info) => { runtimeInfo = info; @@ -552,8 +553,7 @@ describe('CLI', () => { }); it('should not display anything', () => { - // At the "error" level, complete should not be shown - assert.isOk(runtimeInfo.dredd.stdout.indexOf('complete') === -1); + assert.notInclude(runtimeInfo.dredd.output, 'info:'); }); }); diff --git a/test/integration/dredd-test.js b/test/integration/dredd-test.js index 273fe0b96..2d44e843f 100644 --- a/test/integration/dredd-test.js +++ b/test/integration/dredd-test.js @@ -2,36 +2,20 @@ const bodyParser = require('body-parser'); const clone = require('clone'); const express = require('express'); const fs = require('fs'); -const proxyquire = require('proxyquire').noCallThru(); -const sinon = require('sinon'); const { assert } = require('chai'); -const loggerStub = require('../../lib/logger'); +const logger = require('../../lib/logger'); +const reporterOutputLogger = require('../../lib/reporters/reporterOutputLogger'); +const Dredd = require('../../lib/Dredd'); const PORT = 9876; let exitStatus; -let stderr = ''; -let stdout = ''; - -const addHooksStub = proxyquire('../../lib/addHooks', { - './logger': loggerStub, -}); - -const transactionRunner = proxyquire('../../lib/TransactionRunner', { - './addHooks': addHooksStub, - './logger': loggerStub, -}); - -const Dredd = proxyquire('../../lib/Dredd', { - './TransactionRunner': transactionRunner, - './logger': loggerStub, -}); +let output = ''; function execCommand(options = {}, cb) { - stdout = ''; - stderr = ''; + output = ''; exitStatus = null; let finished = false; if (!options.server) { options.server = `http://127.0.0.1:${PORT}`; } @@ -40,41 +24,36 @@ function execCommand(options = {}, cb) { if (!finished) { finished = true; if (error ? error.message : undefined) { - stderr += error.message; + output += error.message; } exitStatus = (error || (((1 * stats.failures) + (1 * stats.errors)) > 0)) ? 1 : 0; - cb(null, stdout, stderr, exitStatus); + cb(); } }); } + +function record(transport, level, message) { + output += `\n${level}: ${message}`; +} + + describe('Dredd class Integration', () => { before(() => { - ['warn', 'error'].forEach((method) => { - sinon.stub(loggerStub, method).callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; }); - }); - [ - 'log', 'info', 'silly', 'verbose', 'test', - 'hook', 'complete', 'pass', 'skip', 'debug', - 'fail', 'request', 'expected', 'actual', - ].forEach((method) => { - sinon.stub(loggerStub, method).callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; }); - }); + logger.transports.console.silent = true; + logger.on('logging', record); + + reporterOutputLogger.transports.console.silent = true; + reporterOutputLogger.on('logging', record); }); after(() => { - ['warn', 'error'].forEach((method) => { - loggerStub[method].restore(); - }); - [ - 'log', 'info', 'silly', 'verbose', 'test', - 'hook', 'complete', 'pass', 'skip', 'debug', - 'fail', 'request', 'expected', 'actual', - ].forEach((method) => { - loggerStub[method].restore(); - }); - }); + logger.transports.console.silent = false; + logger.removeListener('logging', record); + reporterOutputLogger.transports.console.silent = false; + reporterOutputLogger.removeListener('logging', record); + }); describe('when creating Dredd instance with existing API description document and responding server', () => { describe('when the server is responding as specified in the API description', () => { @@ -181,7 +160,7 @@ describe('Dredd class Integration', () => { }); }); - it('should not print warning about missing Apiary API settings', () => assert.notInclude(stderr, 'Apiary API Key or API Project Subdomain were not provided.')); + it('should not print warning about missing Apiary API settings', () => assert.notInclude(output, 'Apiary API Key or API Project Subdomain were not provided.')); it('should contain Authentication header thanks to apiaryApiKey and apiaryApiName configuration', () => { assert.propertyVal(receivedHeaders, 'authentication', 'Token the-key'); @@ -193,7 +172,7 @@ describe('Dredd class Integration', () => { assert.propertyVal(receivedRequestTestRuns, 'public', false); }); - it('should print using the new reporter', () => assert.include(stdout, 'http://url.me/test/run/1234_id')); + it('should print using the new reporter', () => assert.include(output, 'http://url.me/test/run/1234_id')); it('should send results from Gavel', () => { assert.isObject(receivedRequest); @@ -276,7 +255,7 @@ describe('Dredd class Integration', () => { server2.on('close', done); }); - it('should print using the reporter', () => assert.include(stdout, 'http://url.me/test/run/1234_id')); + it('should print using the reporter', () => assert.include(output, 'http://url.me/test/run/1234_id')); it('should send results from gavel', () => { assert.isObject(receivedRequest); @@ -338,11 +317,11 @@ describe('Dredd class Integration', () => { server.on('close', done); }); - it('should print warning about missing Apiary API settings', () => assert.include(stderr, 'Apiary API Key or API Project Subdomain were not provided.')); + it('should print warning about missing Apiary API settings', () => assert.include(output, 'Apiary API Key or API Project Subdomain were not provided.')); - it('should print link to documentation', () => assert.include(stderr, 'https://dredd.org/en/latest/how-to-guides/#using-apiary-reporter-and-apiary-tests')); + it('should print link to documentation', () => assert.include(output, 'https://dredd.org/en/latest/how-to-guides/#using-apiary-reporter-and-apiary-tests')); - it('should print using the new reporter', () => assert.include(stdout, 'http://url.me/test/run/1234_id')); + it('should print using the new reporter', () => assert.include(output, 'http://url.me/test/run/1234_id')); it('should send results from Gavel', () => { assert.isObject(receivedRequest); @@ -426,10 +405,10 @@ describe('Dredd class Integration', () => { it('should exit with status 1', () => assert.equal(exitStatus, 1)); - it('should print error message to stderr', () => { - assert.include(stderr, 'Error when loading file from URL'); - assert.include(stderr, 'Is the provided URL correct?'); - assert.include(stderr, 'connection-error.apib'); + it('should print error message to the output', () => { + assert.include(output, 'Error when loading file from URL'); + assert.include(output, 'Is the provided URL correct?'); + assert.include(output, 'connection-error.apib'); }); }); @@ -444,10 +423,10 @@ describe('Dredd class Integration', () => { it('should exit with status 1', () => assert.equal(exitStatus, 1)); - it('should print error message to stderr', () => { - assert.include(stderr, 'Unable to load file from URL'); - assert.include(stderr, 'responded with status code 404'); - assert.include(stderr, 'not-found.apib'); + it('should print error message to the output', () => { + assert.include(output, 'Unable to load file from URL'); + assert.include(output, 'responded with status code 404'); + assert.include(output, 'not-found.apib'); }); }); @@ -463,7 +442,7 @@ describe('Dredd class Integration', () => { }); describe('when OpenAPI 2 document has multiple responses', () => { - const reTransaction = /(\w+): (\w+) \((\d+)\) \/honey/g; + const reTransaction = /(skip|fail): (\w+) \((\d+)\) \/honey/g; let actual; before(done => execCommand({ @@ -475,7 +454,7 @@ describe('Dredd class Integration', () => { let groups; const matches = []; // eslint-disable-next-line - while (groups = reTransaction.exec(stdout)) { matches.push(groups); } + while (groups = reTransaction.exec(output)) { matches.push(groups); } actual = matches.map((match) => { const keyMap = { 0: 'name', 1: 'action', 2: 'method', 3: 'statusCode', @@ -499,7 +478,7 @@ describe('Dredd class Integration', () => { }); describe('when OpenAPI 2 document has multiple responses and hooks unskip some of them', () => { - const reTransaction = /(\w+): (\w+) \((\d+)\) \/honey/g; + const reTransaction = /(skip|fail): (\w+) \((\d+)\) \/honey/g; let actual; before(done => execCommand({ @@ -512,7 +491,7 @@ describe('Dredd class Integration', () => { let groups; const matches = []; // eslint-disable-next-line - while (groups = reTransaction.exec(stdout)) { matches.push(groups); } + while (groups = reTransaction.exec(output)) { matches.push(groups); } actual = matches.map((match) => { const keyMap = { 0: 'name', 1: 'action', 2: 'method', 3: 'statusCode', @@ -552,7 +531,7 @@ describe('Dredd class Integration', () => { let groups; matches = []; // eslint-disable-next-line - while (groups = reTransactionName.exec(stdout)) { matches.push(groups[1]); } + while (groups = reTransactionName.exec(output)) { matches.push(groups[1]); } done(err); })); diff --git a/test/integration/helpers.js b/test/integration/helpers.js index 79a14c37f..ee78fe6ab 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -9,6 +9,7 @@ const ps = require('ps-node'); const spawn = require('cross-spawn'); const logger = require('../../lib/logger'); +const reporterOutputLogger = require('../../lib/reporters/reporterOutputLogger'); const DEFAULT_SERVER_PORT = 9876; const DREDD_BIN = require.resolve('../../bin/dredd'); @@ -22,16 +23,28 @@ const DREDD_BIN = require.resolve('../../bin/dredd'); // from the 'fn' function // - logging (string) - the recorded logging output function recordLogging(fn, callback) { - const silent = !!logger.transports.console.silent; - logger.transports.console.silent = true; // Supress Dredd's console output (remove if debugging) + const loggerSilent = !!logger.transports.console.silent; + const reporterOutputLoggerSilent = !!reporterOutputLogger.transports.console.silent; + + // Supress Dredd's console output (remove if debugging) + logger.transports.console.silent = true; + reporterOutputLogger.transports.console.silent = true; let logging = ''; - const record = (transport, level, message) => { logging += `${level}: ${message}\n`; }; + const record = (transport, level, message) => { + logging += `${level}: ${message}\n`; + }; logger.on('logging', record); + reporterOutputLogger.on('logging', record); + fn((...args) => { logger.removeListener('logging', record); - logger.transports.console.silent = silent; + logger.transports.console.silent = loggerSilent; + + reporterOutputLogger.removeListener('logging', record); + reporterOutputLogger.transports.console.silent = reporterOutputLoggerSilent; + callback(null, args, logging); }); } diff --git a/test/unit/CLI-test.js b/test/unit/CLI-test.js index 3eaf7909b..4b73cdc44 100644 --- a/test/unit/CLI-test.js +++ b/test/unit/CLI-test.js @@ -64,28 +64,19 @@ function execCommand(custom = {}, cb) { describe('CLI class', () => { before(() => { ['warn', 'error'].forEach((method) => { - sinon.stub(loggerStub, method).callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; }); + sinon + .stub(loggerStub, method) + .callsFake((chunk) => { stderr += `\n${method}: ${chunk}`; }); }); - - [ - 'log', 'info', 'silly', 'verbose', 'test', - 'hook', 'complete', 'pass', 'skip', 'debug', - 'fail', 'request', 'expected', 'actual', - ].forEach((method) => { - sinon.stub(loggerStub, method).callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; }); + ['log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => { + sinon + .stub(loggerStub, method) + .callsFake((chunk) => { stdout += `\n${method}: ${chunk}`; }); }); }); after(() => { - ['warn', 'error'].forEach((method) => { - loggerStub[method].restore(); - }); - - [ - 'log', 'info', 'silly', 'verbose', 'test', - 'hook', 'complete', 'pass', 'skip', 'debug', - 'fail', 'request', 'expected', 'actual', - ].forEach((method) => { + ['warn', 'error', 'log', 'info', 'silly', 'verbose', 'debug'].forEach((method) => { loggerStub[method].restore(); }); }); diff --git a/test/unit/hooksLog-test.js b/test/unit/hooksLog-test.js index 502b85cdd..4d84fc933 100644 --- a/test/unit/hooksLog-test.js +++ b/test/unit/hooksLog-test.js @@ -5,7 +5,7 @@ const util = require('util'); const { assert } = require('chai'); const hooksLog = require('../../lib/hooksLog'); -const loggerStub = require('../../lib/logger'); +const reporterOutputLoggerStub = require('../../lib/reporters/reporterOutputLogger'); describe('hooksLog()', () => { const exampleLogs = [ @@ -13,21 +13,17 @@ describe('hooksLog()', () => { ]; before(() => { - sinon.stub(loggerStub, 'log').callsFake(() => { }); - sinon.stub(loggerStub, 'debug').callsFake(() => { }); - sinon.stub(loggerStub, 'hook').callsFake(() => { }); + sinon.stub(reporterOutputLoggerStub, 'hook').callsFake(() => { }); }); after(() => { - loggerStub.log.restore(); - loggerStub.debug.restore(); - loggerStub.hook.restore(); + reporterOutputLoggerStub.hook.restore(); }); it('should print using util.format only when content is an object type', () => { - const data = hooksLog(clone(exampleLogs), loggerStub, { hello: 'object world' }); - assert.equal(loggerStub.hook.callCount, 1); - assert.deepEqual(loggerStub.hook.getCall(0).args[0], { hello: 'object world' }); + const data = hooksLog(clone(exampleLogs), reporterOutputLoggerStub, { hello: 'object world' }); + assert.equal(reporterOutputLoggerStub.hook.callCount, 1); + assert.deepEqual(reporterOutputLoggerStub.hook.getCall(0).args[0], { hello: 'object world' }); assert.lengthOf(data, 2); assert.isObject(data[1]); assert.property(data[1], 'content'); @@ -38,14 +34,12 @@ describe('hooksLog()', () => { describe('functionality', () => { beforeEach(() => { - loggerStub.log.resetHistory(); - loggerStub.debug.resetHistory(); - loggerStub.hook.resetHistory(); + reporterOutputLoggerStub.hook.resetHistory(); }); it('should push message to the passed array and return the new array', () => { const originLogs = []; - const data = hooksLog(originLogs, loggerStub, 'one message'); + const data = hooksLog(originLogs, reporterOutputLoggerStub, 'one message'); assert.isArray(data); assert.lengthOf(data, 1); assert.strictEqual(data, originLogs); @@ -55,7 +49,7 @@ describe('hooksLog()', () => { it('should push message to undefined logs and return new array instead', () => { const originLogs = undefined; - const data = hooksLog(originLogs, loggerStub, 'another message'); + const data = hooksLog(originLogs, reporterOutputLoggerStub, 'another message'); assert.isArray(data); assert.lengthOf(data, 1); assert.isUndefined(originLogs); @@ -65,7 +59,7 @@ describe('hooksLog()', () => { it('should append message to an existing logs array', () => { const originLogs = clone(exampleLogs); - const data = hooksLog(originLogs, loggerStub, 'some other idea'); + const data = hooksLog(originLogs, reporterOutputLoggerStub, 'some other idea'); assert.isArray(data); assert.lengthOf(data, 2); assert.deepEqual(data, originLogs); @@ -74,15 +68,12 @@ describe('hooksLog()', () => { }); it('should use "hook" logger level', () => { - hooksLog([], loggerStub, 'there is a log'); + hooksLog([], reporterOutputLoggerStub, 'there is a log'); - assert.isTrue(loggerStub.hook.called); - assert.equal(loggerStub.hook.callCount, 1); + assert.isTrue(reporterOutputLoggerStub.hook.called); + assert.equal(reporterOutputLoggerStub.hook.callCount, 1); - assert.isFalse(loggerStub.log.called); - assert.isFalse(loggerStub.debug.called); - - assert.equal(loggerStub.hook.getCall(0).args[0], 'there is a log'); + assert.equal(reporterOutputLoggerStub.hook.getCall(0).args[0], 'there is a log'); }); }); }); diff --git a/test/unit/reporters/ApiaryReporter-test.js b/test/unit/reporters/ApiaryReporter-test.js index 582038dfb..e7204dab0 100644 --- a/test/unit/reporters/ApiaryReporter-test.js +++ b/test/unit/reporters/ApiaryReporter-test.js @@ -7,9 +7,11 @@ const { EventEmitter } = require('events'); const blueprintData = require('../../fixtures/blueprint-data'); const loggerStub = require('../../../lib/logger'); +const reporterOutputLoggerStub = require('../../../lib/reporters/reporterOutputLogger'); const ApiaryReporter = proxyquire('../../../lib/reporters/ApiaryReporter', { '../logger': loggerStub, + './reporterOutputLogger': reporterOutputLoggerStub, }); const PORT = 9876; @@ -18,21 +20,13 @@ nock.enableNetConnect(); describe('ApiaryReporter', () => { let env = {}; beforeEach(() => { - sinon.stub(loggerStub, 'info'); - sinon.stub(loggerStub, 'complete'); - sinon.stub(loggerStub, 'error'); - sinon.stub(loggerStub, 'warn'); - sinon.stub(loggerStub, 'log'); sinon.stub(loggerStub, 'verbose'); + sinon.stub(reporterOutputLoggerStub, 'complete'); }); afterEach(() => { - sinon.stub(loggerStub.info.restore()); - sinon.stub(loggerStub.complete.restore()); - sinon.stub(loggerStub.error.restore()); - sinon.stub(loggerStub.warn.restore()); - sinon.stub(loggerStub.log.restore()); sinon.stub(loggerStub.verbose.restore()); + sinon.stub(reporterOutputLoggerStub.complete.restore()); }); before(() => nock.disableNetConnect()); @@ -692,7 +686,7 @@ describe('ApiaryReporter', () => { const apiaryReporter = new ApiaryReporter(emitter, {}, {}, { custom: { apiaryReporterEnv: env } }); apiaryReporter.remoteId = runId; emitter.emit('end', () => { - assert.isOk(loggerStub.complete.calledWith('See results in Apiary at: https://app.apiary.io/public/tests/run/507f1f77bcf86cd799439011')); + assert.isOk(reporterOutputLoggerStub.complete.calledWith('See results in Apiary at: https://app.apiary.io/public/tests/run/507f1f77bcf86cd799439011')); done(); }); }); @@ -703,7 +697,7 @@ describe('ApiaryReporter', () => { apiaryReporter.remoteId = runId; apiaryReporter.reportUrl = 'https://absolutely.fancy.url/wich-can-change/some/id'; emitter.emit('end', () => { - assert.isOk(loggerStub.complete.calledWith('See results in Apiary at: https://absolutely.fancy.url/wich-can-change/some/id')); + assert.isOk(reporterOutputLoggerStub.complete.calledWith('See results in Apiary at: https://absolutely.fancy.url/wich-can-change/some/id')); done(); }); }); @@ -1002,7 +996,7 @@ describe('ApiaryReporter', () => { const apiaryReporter = new ApiaryReporter(emitter, {}, {}, { custom: { apiaryReporterEnv: env } }); apiaryReporter.remoteId = runId; emitter.emit('end', () => { - assert.isOk(loggerStub.complete.calledWith('See results in Apiary at: https://app.apiary.io/jakubtest/tests/run/507f1f77bcf86cd799439011')); + assert.isOk(reporterOutputLoggerStub.complete.calledWith('See results in Apiary at: https://app.apiary.io/jakubtest/tests/run/507f1f77bcf86cd799439011')); done(); }); }); @@ -1013,7 +1007,7 @@ describe('ApiaryReporter', () => { apiaryReporter.remoteId = runId; apiaryReporter.reportUrl = 'https://absolutely.fancy.url/wich-can-change/some/id'; emitter.emit('end', () => { - assert.isOk(loggerStub.complete.calledWith('See results in Apiary at: https://absolutely.fancy.url/wich-can-change/some/id')); + assert.isOk(reporterOutputLoggerStub.complete.calledWith('See results in Apiary at: https://absolutely.fancy.url/wich-can-change/some/id')); done(); }); }); diff --git a/test/unit/reporters/CLIReporter-test.js b/test/unit/reporters/CLIReporter-test.js index 31a45bd2e..fef1c8ddc 100644 --- a/test/unit/reporters/CLIReporter-test.js +++ b/test/unit/reporters/CLIReporter-test.js @@ -4,17 +4,25 @@ const { assert } = require('chai'); const { EventEmitter } = require('events'); const loggerStub = require('../../../lib/logger'); +const reporterOutputLoggerStub = require('../../../lib/reporters/reporterOutputLogger'); const CLIReporter = proxyquire('../../../lib/reporters/CLIReporter', { '../logger': loggerStub, + './reporterOutputLogger': reporterOutputLoggerStub, }); describe('CLIReporter', () => { let test = {}; - before(() => { loggerStub.transports.console.silent = true; }); + before(() => { + loggerStub.transports.console.silent = true; + reporterOutputLoggerStub.transports.console.silent = true; + }); - after(() => { loggerStub.transports.console.silent = false; }); + after(() => { + loggerStub.transports.console.silent = false; + reporterOutputLoggerStub.transports.console.silent = false; + }); describe('when starting', () => { beforeEach(() => sinon.spy(loggerStub, 'info')); @@ -39,27 +47,27 @@ describe('CLIReporter', () => { }; }); - beforeEach(() => sinon.spy(loggerStub, 'pass')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'pass')); - afterEach(() => loggerStub.pass.restore()); + afterEach(() => reporterOutputLoggerStub.pass.restore()); it('should write pass to the console', () => { const emitter = new EventEmitter(); (new CLIReporter(emitter, {}, {}, true)); emitter.emit('test pass', test); - assert.isOk(loggerStub.pass.calledOnce); + assert.isOk(reporterOutputLoggerStub.pass.calledOnce); }); describe('when details=true', () => { - beforeEach(() => sinon.spy(loggerStub, 'request')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'request')); - afterEach(() => loggerStub.request.restore()); + afterEach(() => reporterOutputLoggerStub.request.restore()); it('should write details for passing tests', () => { const emitter = new EventEmitter(); (new CLIReporter(emitter, {}, {}, true, true)); emitter.emit('test pass', test); - assert.isOk(loggerStub.request.calledOnce); + assert.isOk(reporterOutputLoggerStub.request.calledOnce); }); }); }); @@ -73,28 +81,28 @@ describe('CLIReporter', () => { }); describe('when errors are inline', () => { - beforeEach(() => sinon.spy(loggerStub, 'fail')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'fail')); - afterEach(() => loggerStub.fail.restore()); + afterEach(() => reporterOutputLoggerStub.fail.restore()); it('should write fail to the console', () => { const emitter = new EventEmitter(); (new CLIReporter(emitter, {}, {}, true)); emitter.emit('test fail', test); - assert.isOk(loggerStub.fail.calledTwice); + assert.isOk(reporterOutputLoggerStub.fail.calledTwice); }); }); describe('when errors are aggregated', () => { - beforeEach(() => sinon.spy(loggerStub, 'fail')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'fail')); - afterEach(() => loggerStub.fail.restore()); + afterEach(() => reporterOutputLoggerStub.fail.restore()); it('should not write full failure to the console at the time of failure', () => { const emitter = new EventEmitter(); (new CLIReporter(emitter, {}, {}, false)); emitter.emit('test fail', test); - assert.isOk(loggerStub.fail.calledOnce); + assert.isOk(reporterOutputLoggerStub.fail.calledOnce); }); it('should write full failure to the console after execution is complete', (done) => { @@ -102,7 +110,7 @@ describe('CLIReporter', () => { const cliReporter = new CLIReporter(emitter, {}, {}, false); cliReporter.errors = [test]; emitter.emit('end', () => { - assert.isOk(loggerStub.fail.calledTwice); + assert.isOk(reporterOutputLoggerStub.fail.calledTwice); done(); }); }); @@ -117,15 +125,15 @@ describe('CLIReporter', () => { }; }); - beforeEach(() => sinon.spy(loggerStub, 'error')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'error')); - afterEach(() => loggerStub.error.restore()); + afterEach(() => reporterOutputLoggerStub.error.restore()); it('should write error to the console', () => { const emitter = new EventEmitter(); (new CLIReporter(emitter, {}, {}, false)); emitter.emit('test error', new Error('Error'), test); - assert.isOk(loggerStub.error.calledTwice); + assert.isOk(reporterOutputLoggerStub.error.calledTwice); }); }); @@ -138,9 +146,9 @@ describe('CLIReporter', () => { }; }); - beforeEach(() => sinon.spy(loggerStub, 'error')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'error')); - afterEach(() => loggerStub.error.restore()); + afterEach(() => reporterOutputLoggerStub.error.restore()); const connectionErrors = ['ECONNRESET', 'ENOTFOUND', 'ESOCKETTIMEDOUT', 'ETIMEDOUT', 'ECONNREFUSED', 'EHOSTUNREACH', 'EPIPE']; @@ -151,7 +159,7 @@ describe('CLIReporter', () => { error.code = errType; emitter.emit('test error', error, test); - const messages = Object.keys(loggerStub.error.args).map((value, index) => loggerStub.error.args[index][0]); + const messages = Object.keys(reporterOutputLoggerStub.error.args).map((value, index) => reporterOutputLoggerStub.error.args[index][0]); assert.include(messages.join(), 'Error connecting'); }))); @@ -165,15 +173,15 @@ describe('CLIReporter', () => { }; }); - beforeEach(() => sinon.spy(loggerStub, 'skip')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'skip')); - afterEach(() => loggerStub.skip.restore()); + afterEach(() => reporterOutputLoggerStub.skip.restore()); it('should write skip to the console', () => { const emitter = new EventEmitter(); (new CLIReporter(emitter, {}, {}, false)); emitter.emit('test skip', test); - assert.isOk(loggerStub.skip.calledOnce); + assert.isOk(reporterOutputLoggerStub.skip.calledOnce); }); }); @@ -186,9 +194,9 @@ describe('CLIReporter', () => { }; }); - beforeEach(() => sinon.spy(loggerStub, 'complete')); + beforeEach(() => sinon.spy(reporterOutputLoggerStub, 'complete')); - afterEach(() => loggerStub.complete.restore()); + afterEach(() => reporterOutputLoggerStub.complete.restore()); describe('when there is at least one test', () => it('should write to the console', (done) => { const emitter = new EventEmitter(); @@ -196,7 +204,7 @@ describe('CLIReporter', () => { cliReporter.tests = [test]; cliReporter.stats.tests = 1; emitter.emit('end', () => { - assert.isOk(loggerStub.complete.calledTwice); + assert.isOk(reporterOutputLoggerStub.complete.calledTwice); done(); }); })); @@ -205,7 +213,7 @@ describe('CLIReporter', () => { const emitter = new EventEmitter(); (new CLIReporter(emitter, {}, {}, false)); emitter.emit('end', () => { - assert.isOk(loggerStub.complete.calledOnce); + assert.isOk(reporterOutputLoggerStub.complete.calledOnce); done(); }); })); diff --git a/test/unit/reporters/DotReporter-test.js b/test/unit/reporters/DotReporter-test.js index 052ae1fe1..d90acb418 100644 --- a/test/unit/reporters/DotReporter-test.js +++ b/test/unit/reporters/DotReporter-test.js @@ -5,6 +5,7 @@ const { assert } = require('chai'); const { EventEmitter } = require('events'); const loggerStub = require('../../../lib/logger'); +const reporterOutputLoggerStub = require('../../../lib/reporters/reporterOutputLogger'); const DotReporter = proxyquire('../../../lib/reporters/DotReporter', { '../logger': loggerStub, @@ -48,16 +49,16 @@ describe('DotReporter', () => { describe('when ending', () => { beforeEach(() => { stats.tests = 1; - sinon.spy(loggerStub, 'complete'); + sinon.spy(reporterOutputLoggerStub, 'complete'); sinon.stub(dotReporter, 'write'); }); afterEach(() => { - loggerStub.complete.restore(); + reporterOutputLoggerStub.complete.restore(); dotReporter.write.restore(); }); - it('should log that testing is complete', () => emitter.emit('end', () => assert.isOk(loggerStub.complete.calledTwice))); + it('should log that testing is complete', () => emitter.emit('end', () => assert.isOk(reporterOutputLoggerStub.complete.calledTwice))); describe('when there are failures', () => { before(() => { @@ -71,13 +72,13 @@ describe('DotReporter', () => { dotReporter.errors = [test]; dotReporter.stats.tests = 1; emitter.emit('test start', test); - sinon.spy(loggerStub, 'fail'); + sinon.spy(reporterOutputLoggerStub, 'fail'); }); - afterEach(() => loggerStub.fail.restore()); + afterEach(() => reporterOutputLoggerStub.fail.restore()); it('should log the failures at the end of testing', done => emitter.emit('end', () => { - assert.isOk(loggerStub.fail.called); + assert.isOk(reporterOutputLoggerStub.fail.called); done(); })); }); diff --git a/test/unit/reporters/HTMLReporter-test.js b/test/unit/reporters/HTMLReporter-test.js index 644e81a83..f0366e9b0 100644 --- a/test/unit/reporters/HTMLReporter-test.js +++ b/test/unit/reporters/HTMLReporter-test.js @@ -6,11 +6,13 @@ const { assert } = require('chai'); const { EventEmitter } = require('events'); const loggerStub = require('../../../lib/logger'); +const reporterOutputLoggerStub = require('../../../lib/reporters/reporterOutputLogger'); const fsExtraStub = { mkdirp(path, cb) { return cb(); } }; const HTMLReporter = proxyquire('../../../lib/reporters/HTMLReporter', { '../logger': loggerStub, + './reporterOutputLogger': reporterOutputLoggerStub, fs: fsStub, 'fs-extra': fsExtraStub, }); @@ -22,9 +24,15 @@ describe('HTMLReporter', () => { let test = {}; let tests; - before(() => { loggerStub.transports.console.silent = true; }); + before(() => { + loggerStub.transports.console.silent = true; + reporterOutputLoggerStub.transports.console.silent = true; + }); - after(() => { loggerStub.transports.console.silent = false; }); + after(() => { + loggerStub.transports.console.silent = false; + reporterOutputLoggerStub.transports.console.silent = false; + }); beforeEach(() => { emitter = new EventEmitter(); @@ -46,15 +54,15 @@ describe('HTMLReporter', () => { describe('when file exists', () => { before(() => { sinon.stub(fsStub, 'existsSync').callsFake(() => true); - sinon.stub(loggerStub, 'info'); + sinon.stub(loggerStub, 'warn'); }); after(() => { fsStub.existsSync.restore(); - loggerStub.info.restore(); + loggerStub.warn.restore(); }); - it('should inform about the existing file', () => assert.isOk(loggerStub.info.called)); + it('should warn about the existing file', () => assert.isOk(loggerStub.warn.called)); }); describe('when file does not exist', () => { @@ -100,13 +108,13 @@ describe('HTMLReporter', () => { describe('when cannot create output directory', () => { beforeEach(() => { - sinon.stub(loggerStub, 'error'); + sinon.stub(reporterOutputLoggerStub, 'error'); sinon.stub(fsStub, 'writeFile').callsFake((path, data, callback) => callback()); sinon.stub(fsExtraStub, 'mkdirp').callsFake((path, cb) => cb('error')); }); after(() => { - loggerStub.error.restore(); + reporterOutputLoggerStub.error.restore(); fsStub.writeFile.restore(); fsExtraStub.mkdirp.restore(); }); @@ -114,7 +122,7 @@ describe('HTMLReporter', () => { it('should write to log', done => emitter.emit('end', () => { assert.isOk(fsExtraStub.mkdirp.called); assert.isOk(fsStub.writeFile.notCalled); - assert.isOk(loggerStub.error.called); + assert.isOk(reporterOutputLoggerStub.error.called); done(); })); }); diff --git a/test/unit/reporters/MarkdownReporter-test.js b/test/unit/reporters/MarkdownReporter-test.js index d33db1148..8c020400b 100644 --- a/test/unit/reporters/MarkdownReporter-test.js +++ b/test/unit/reporters/MarkdownReporter-test.js @@ -6,11 +6,13 @@ const { assert } = require('chai'); const { EventEmitter } = require('events'); const loggerStub = require('../../../lib/logger'); +const reporterOutputLoggerStub = require('../../../lib/reporters/reporterOutputLogger'); const fsExtraStub = { mkdirp(path, cb) { return cb(); } }; const MarkdownReporter = proxyquire('../../../lib/reporters/MarkdownReporter', { '../logger': loggerStub, + './reporterOutputLogger': reporterOutputLoggerStub, fs: fsStub, 'fs-extra': fsExtraStub, }); @@ -22,9 +24,15 @@ describe('MarkdownReporter', () => { let test = {}; let tests; - before(() => { loggerStub.transports.console.silent = true; }); + before(() => { + loggerStub.transports.console.silent = true; + reporterOutputLoggerStub.transports.console.silent = true; + }); - after(() => { loggerStub.transports.console.silent = false; }); + after(() => { + loggerStub.transports.console.silent = false; + reporterOutputLoggerStub.transports.console.silent = false; + }); beforeEach(() => { emitter = new EventEmitter(); @@ -47,15 +55,15 @@ describe('MarkdownReporter', () => { describe('when file exists', () => { before(() => { sinon.stub(fsStub, 'existsSync').callsFake(() => true); - sinon.stub(loggerStub, 'info'); + sinon.stub(loggerStub, 'warn'); }); after(() => { fsStub.existsSync.restore(); - loggerStub.info.restore(); + loggerStub.warn.restore(); }); - it('should inform about the existing file', () => assert.isOk(loggerStub.info.called)); + it('should warn about the existing file', () => assert.isOk(loggerStub.warn.called)); }); describe('when file does not exist', () => { @@ -104,20 +112,20 @@ describe('MarkdownReporter', () => { describe('when cannot create output directory', () => { beforeEach(() => { sinon.stub(fsStub, 'writeFile'); - sinon.stub(loggerStub, 'error'); + sinon.stub(reporterOutputLoggerStub, 'error'); sinon.stub(fsExtraStub, 'mkdirp').callsFake((path, cb) => cb('error')); }); after(() => { fsStub.writeFile.restore(); - loggerStub.error.restore(); + reporterOutputLoggerStub.error.restore(); fsExtraStub.mkdirp.restore(); }); it('should write to log', done => emitter.emit('end', () => { assert.isOk(fsExtraStub.mkdirp.called); assert.isOk(fsStub.writeFile.notCalled); - assert.isOk(loggerStub.error.called); + assert.isOk(reporterOutputLoggerStub.error.called); done(); })); }); diff --git a/test/unit/reporters/NyanReporter-test.js b/test/unit/reporters/NyanReporter-test.js index 657ada07f..d1ab5b0e7 100644 --- a/test/unit/reporters/NyanReporter-test.js +++ b/test/unit/reporters/NyanReporter-test.js @@ -4,10 +4,10 @@ const sinon = require('sinon'); const { assert } = require('chai'); const { EventEmitter } = require('events'); -const loggerStub = require('../../../lib/logger'); +const reporterOutputLoggerStub = require('../../../lib/reporters/reporterOutputLogger'); const NyanCatReporter = proxyquire('../../../lib/reporters/NyanReporter', { - '../logger': loggerStub, + './reporterOutputLogger': reporterOutputLoggerStub, }); describe('NyanCatReporter', () => { @@ -16,9 +16,13 @@ describe('NyanCatReporter', () => { let tests; let nyanReporter; - before(() => { loggerStub.transports.console.silent = true; }); + before(() => { + reporterOutputLoggerStub.transports.console.silent = true; + }); - after(() => { loggerStub.transports.console.silent = false; }); + after(() => { + reporterOutputLoggerStub.transports.console.silent = false; + }); beforeEach(() => { emitter = new EventEmitter(); @@ -58,19 +62,19 @@ describe('NyanCatReporter', () => { describe('when ending', () => { beforeEach(() => { - sinon.spy(loggerStub, 'complete'); + sinon.spy(reporterOutputLoggerStub, 'complete'); sinon.spy(nyanReporter, 'draw'); sinon.stub(nyanReporter, 'write'); }); afterEach(() => { - loggerStub.complete.restore(); + reporterOutputLoggerStub.complete.restore(); nyanReporter.draw.restore(); nyanReporter.write.restore(); }); it('should log that testing is complete', done => emitter.emit('end', () => { - assert.isOk(loggerStub.complete.calledTwice); + assert.isOk(reporterOutputLoggerStub.complete.calledTwice); done(); })); @@ -82,13 +86,13 @@ describe('NyanCatReporter', () => { }; nyanReporter.errors = [test]; emitter.emit('test start', test); - sinon.spy(loggerStub, 'fail'); + sinon.spy(reporterOutputLoggerStub, 'fail'); }); - afterEach(() => loggerStub.fail.restore()); + afterEach(() => reporterOutputLoggerStub.fail.restore()); it('should log the failures at the end of testing', done => emitter.emit('end', () => { - assert.isOk(loggerStub.fail.calledTwice); + assert.isOk(reporterOutputLoggerStub.fail.calledTwice); done(); })); }); diff --git a/test/unit/reporters/XUnitReporter-test.js b/test/unit/reporters/XUnitReporter-test.js index db911539d..59566fb22 100644 --- a/test/unit/reporters/XUnitReporter-test.js +++ b/test/unit/reporters/XUnitReporter-test.js @@ -6,11 +6,13 @@ const { assert } = require('chai'); const { EventEmitter } = require('events'); const loggerStub = require('../../../lib/logger'); +const reporterOutputLoggerStub = require('../../../lib/reporters/reporterOutputLogger'); const fsExtraStub = { mkdirp(path, cb) { return cb(); } }; const XUnitReporter = proxyquire('../../../lib/reporters/XUnitReporter', { '../logger': loggerStub, + './reporterOutputLogger': reporterOutputLoggerStub, fs: fsStub, 'fs-extra': fsExtraStub, }); @@ -18,28 +20,34 @@ const XUnitReporter = proxyquire('../../../lib/reporters/XUnitReporter', { describe('XUnitReporter', () => { let test = {}; - before(() => { loggerStub.transports.console.silent = true; }); + before(() => { + loggerStub.transports.console.silent = true; + reporterOutputLoggerStub.transports.console.silent = true; + }); - after(() => { loggerStub.transports.console.silent = false; }); + after(() => { + loggerStub.transports.console.silent = false; + reporterOutputLoggerStub.transports.console.silent = false; + }); describe('when creating', () => { describe('when file exists', () => { before(() => { sinon.stub(fsStub, 'existsSync').callsFake(() => true); sinon.stub(fsStub, 'unlinkSync').callsFake(() => true); - sinon.stub(loggerStub, 'info'); + sinon.stub(loggerStub, 'warn'); }); after(() => { fsStub.existsSync.restore(); fsStub.unlinkSync.restore(); - loggerStub.info.restore(); + loggerStub.warn.restore(); }); - it('should inform about the existing file', () => { + it('should warn about the existing file', () => { const emitter = new EventEmitter(); (new XUnitReporter(emitter, {}, {}, 'test.xml')); - assert.isOk(loggerStub.info.called); + assert.isOk(loggerStub.warn.called); }); }); @@ -88,13 +96,13 @@ describe('XUnitReporter', () => { describe('when cannot create output directory', () => { beforeEach(() => { sinon.stub(fsStub, 'appendFileSync'); - sinon.stub(loggerStub, 'error'); + sinon.stub(reporterOutputLoggerStub, 'error'); sinon.stub(fsExtraStub, 'mkdirp').callsFake((path, cb) => cb('error')); }); after(() => { fsStub.appendFileSync.restore(); - loggerStub.error.restore(); + reporterOutputLoggerStub.error.restore(); fsExtraStub.mkdirp.restore(); }); @@ -104,7 +112,7 @@ describe('XUnitReporter', () => { emitter.emit('start', '', () => { assert.isOk(fsExtraStub.mkdirp.called); assert.isOk(fsStub.appendFileSync.notCalled); - assert.isOk(loggerStub.error.called); + assert.isOk(reporterOutputLoggerStub.error.called); done(); }); });