From f8e2b0fdd145dd97f855bce29e62c56e8ed79869 Mon Sep 17 00:00:00 2001 From: Maximilian Antoni Date: Sat, 28 Mar 2015 21:07:56 +0100 Subject: [PATCH] improve: dry up reporter/base test --- test/reporters/base.js | 106 +++++++++++++---------------------------- 1 file changed, 33 insertions(+), 73 deletions(-) diff --git a/test/reporters/base.js b/test/reporters/base.js index d8ca2c7b78..cb8a6a6707 100644 --- a/test/reporters/base.js +++ b/test/reporters/base.js @@ -1,30 +1,44 @@ var Base = require('../../lib/reporters/base') , Assert = require('assert').AssertionError; +function makeTest(err) { + return { + err: err, + fullTitle: function () { + return 'test title'; + } + }; +} + describe('Base reporter', function () { + var stdout + , stdoutWrite + , useColors; + + beforeEach(function () { + stdout = []; + stdoutWrite = process.stdout.write; + process.stdout.write = function (string) { + stdout.push(string); + }; + useColors = Base.useColors; + Base.useColors = false; + }); + + afterEach(function () { + process.stdout.write = stdoutWrite; + Base.useColors = useColors; + }); describe('showDiff', function() { it('should show diffs by default', function () { var err = new Assert({ actual: 'foo', expected: 'bar' }) - , stdout = [] - , stdoutWrite = process.stdout.write , errOut; - var test = { - err: err, - fullTitle: function () { - return 'test title'; - } - }; - - process.stdout.write = function (string) { - stdout.push(string); - }; + var test = makeTest(err); Base.list([test]); - process.stdout.write = stdoutWrite; - errOut = stdout.join('\n'); errOut.should.match(/actual/); errOut.should.match(/expected/); @@ -32,26 +46,14 @@ describe('Base reporter', function () { it('should show diffs if property set to `true`', function () { var err = new Assert({ actual: 'foo', expected: 'bar' }) - , stdout = [] - , stdoutWrite = process.stdout.write , errOut; err.showDiff = true; - var test = { - err: err, - fullTitle: function () { - return 'test title'; - } - }; + var test = makeTest(err); - process.stdout.write = function (string) { - stdout.push(string); - }; Base.list([test]); - process.stdout.write = stdoutWrite; - errOut = stdout.join('\n'); errOut.should.match(/actual/); errOut.should.match(/expected/); @@ -59,26 +61,13 @@ describe('Base reporter', function () { it('should not show diffs when showDiff property set to `false`', function () { var err = new Assert({ actual: 'foo', expected: 'bar' }) - , stdout = [] - , stdoutWrite = process.stdout.write , errOut; err.showDiff = false; - var test = { - err: err, - fullTitle: function () { - return 'test title'; - } - }; - - process.stdout.write = function (string) { - stdout.push(string); - }; + var test = makeTest(err); Base.list([test]); - process.stdout.write = stdoutWrite; - errOut = stdout.join('\n'); errOut.should.not.match(/actual/); errOut.should.not.match(/expected/); @@ -87,67 +76,38 @@ describe('Base reporter', function () { it('should not stringify strings', function () { var err = new Error('test'), - stdout = [], - stdoutWrite = process.stdout.write, errOut; err.actual = "a1"; err.expected = "e2"; err.showDiff = true; - var test = { - err: err, - fullTitle: function () { - return 'title'; - } - }; - - process.stdout.write = function (string) { - stdout.push(string); - }; + var test = makeTest(err); Base.list([test]); - process.stdout.write = stdoutWrite; - errOut = stdout.join('\n'); - errOut.should.not.match(/"/); errOut.should.match(/test/); errOut.should.match(/actual/); errOut.should.match(/expected/); - }); - it('should stringify objects', function () { var err = new Error('test'), - stdout = [], - stdoutWrite = process.stdout.write, errOut; err.actual = {key:"a1"}; err.expected = {key:"e1"}; err.showDiff = true; - var test = { - err: err, - fullTitle: function () { - return 'title'; - } - }; - - process.stdout.write = function (string) { - stdout.push(string); - }; + var test = makeTest(err); Base.list([test]); - process.stdout.write = stdoutWrite; - errOut = stdout.join('\n'); - errOut.should.match(/"key"/); errOut.should.match(/test/); errOut.should.match(/actual/); errOut.should.match(/expected/); }); + });