Skip to content

Commit

Permalink
improve: dry up reporter/base test
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Mar 28, 2015
1 parent ec6440e commit f8e2b0f
Showing 1 changed file with 33 additions and 73 deletions.
106 changes: 33 additions & 73 deletions test/reporters/base.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,73 @@
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/);
});

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/);
});

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/);
Expand All @@ -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/);
});

});

0 comments on commit f8e2b0f

Please sign in to comment.