Skip to content

Commit

Permalink
Refactor Reporter tests (#3272)
Browse files Browse the repository at this point in the history
* Closes #3260

* refactor-better test harness for reporters tests'

* refactor-better test harness for reporters tests'

* refactor - some additional work

* refactor - helper func

* refactor - helper func

* refactor - helper func

* fix - improvements to helper functions
  • Loading branch information
Jerry Muzsik authored and boneskull committed Apr 1, 2018
1 parent 8e740c5 commit 9ba8eca
Show file tree
Hide file tree
Showing 16 changed files with 374 additions and 647 deletions.
Empty file added nom
Empty file.
131 changes: 53 additions & 78 deletions test/reporters/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,16 @@ var assert = require('assert');
var Base = require('../../lib/reporters/base');
var Assert = require('assert').AssertionError;

function makeTest (err) {
return {
err: err,
titlePath: function () {
return ['test title'];
}
};
}

function createElements (argObj) {
var res = [];
for (var i = argObj.from; i <= argObj.to; i += 1) {
res.push('element ' + i);
}
return res;
}
var makeTest = require('./helpers').makeTest;
var createElements = require('./helpers').createElements;

describe('Base reporter', function () {
var stdout;
var stdoutWrite;
var useColors;
var err;
var errOut;
var test;

beforeEach(function () {
stdout = [];
Expand All @@ -43,11 +32,12 @@ describe('Base reporter', function () {
});

describe('showDiff', function () {
it('should show diffs by default', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' });
var errOut;
beforeEach(function () {
err = new Assert({ actual: 'foo', expected: 'bar' });
});

var test = makeTest(err);
it('should show diffs by default', function () {
test = makeTest(err);

Base.list([test]);

Expand All @@ -57,11 +47,8 @@ describe('Base reporter', function () {
});

it('should show diffs if property set to `true`', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' });
var errOut;

err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -71,11 +58,8 @@ describe('Base reporter', function () {
});

it('should not show diffs when showDiff property set to `false`', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' });
var errOut;

err.showDiff = false;
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -85,10 +69,9 @@ describe('Base reporter', function () {
});

it('should not show diffs when expected is not defined', function () {
var err = new Error('ouch');
var errOut;
err = new Error('ouch');

var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -98,10 +81,7 @@ describe('Base reporter', function () {
});

it('should not show diffs when hideDiff is set', function () {
var err = new Assert({ actual: 'foo', expected: 'bar' });
var errOut;

var test = makeTest(err);
test = makeTest(err);

Base.hideDiff = true;
Base.list([test]);
Expand All @@ -116,13 +96,12 @@ describe('Base reporter', function () {
describe('Getting two strings', function () {
// Fix regression V1.2.1(see: issue #1241)
it('should show strings diff as is', function () {
var err = new Error('test');
var errOut;
err = new Error('test');

err.actual = 'foo\nbar';
err.expected = 'foo\nbaz';
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -138,6 +117,9 @@ describe('Base reporter', function () {

describe('Diff generation', function () {
var oldInlineDiffs;
var actual;
var expected;
var output;

beforeEach(function () {
oldInlineDiffs = Base.inlineDiffs;
Expand All @@ -148,35 +130,34 @@ describe('Base reporter', function () {
});

it('should generate unified diffs if `inlineDiff === false`', function () {
var actual = 'a foo unified diff';
var expected = 'a bar unified diff';
actual = 'a foo unified diff';
expected = 'a bar unified diff';

Base.inlineDiffs = false;
var output = Base.generateDiff(actual, expected);
output = Base.generateDiff(actual, expected);

expect(output).to.equal('\n + expected - actual\n\n -a foo unified diff\n +a bar unified diff\n ');
});

it('should generate inline diffs if `inlineDiffs === true`', function () {
var actual = 'a foo inline diff';
var expected = 'a bar inline diff';
actual = 'a foo inline diff';
expected = 'a bar inline diff';

Base.inlineDiffs = true;
var output = Base.generateDiff(actual, expected);
output = Base.generateDiff(actual, expected);

expect(output).to.equal(' \n actual expected\n \n a foobar inline diff\n ');
});
});

describe('Inline strings diff', function () {
it('should show single line diff if property set to `true`', function () {
var err = new Error('test');
var errOut;
err = new Error('test');

err.actual = 'a foo inline diff';
err.expected = 'a bar inline diff';
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.inlineDiffs = true;
Base.list([test]);
Expand All @@ -190,13 +171,12 @@ describe('Base reporter', function () {
});

it('should split lines when string has more than 4 line breaks', function () {
var err = new Error('test');
var errOut;
err = new Error('test');

err.actual = 'a\nfoo\ninline\ndiff\nwith\nmultiple lines';
err.expected = 'a\nbar\ninline\ndiff\nwith\nmultiple lines';
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.inlineDiffs = true;
Base.list([test]);
Expand All @@ -216,14 +196,15 @@ describe('Base reporter', function () {
});

describe('unified diff reporter', function () {
it('should separate diff hunks by two dashes', function () {
var err = new Error('test');
var errOut;
beforeEach(function () {
err = new Error('test');
});

it('should separate diff hunks by two dashes', function () {
err.actual = createElements({ from: 2, to: 11 });
err.expected = createElements({ from: 1, to: 10 });
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.inlineDiffs = false;
Base.list([test]);
Expand Down Expand Up @@ -256,13 +237,12 @@ describe('Base reporter', function () {
});

it('should stringify objects', function () {
var err = new Error('test');
var errOut;
err = new Error('test');

err.actual = {key: 'a1'};
err.expected = {key: 'e1'};
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -274,15 +254,14 @@ describe('Base reporter', function () {
});

it('should stringify Object.create(null)', function () {
var err = new Error('test');
var errOut;
err = new Error('test');

err.actual = Object.create(null);
err.actual.hasOwnProperty = 1;
err.expected = Object.create(null);
err.expected.hasOwnProperty = 2;
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -294,15 +273,13 @@ describe('Base reporter', function () {
});

it('should handle error messages that are not strings', function () {
var errOut;

try {
assert(false, true);
} catch (err) {
err.actual = false;
err.expected = true;
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -315,8 +292,6 @@ describe('Base reporter', function () {
});

it('should interpret chaijs custom error messages', function () {
var errOut;

try {
// expect(43, 'custom error message').to.equal(42);
// AssertionError: custom error message: expected 43 to equal 42.
Expand All @@ -325,7 +300,7 @@ describe('Base reporter', function () {
err.actual = 43;
err.expected = 42;
err.showDiff = true;
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

Expand All @@ -339,51 +314,51 @@ describe('Base reporter', function () {
});

it('should remove message from stack', function () {
var err = {
err = {
message: 'Error',
stack: 'Error\nfoo\nbar',
showDiff: false
};
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

var errOut = stdout.join('\n').trim();
errOut = stdout.join('\n').trim();
expect(errOut).to.equal('1) test title:\n Error\n foo\n bar');
});

it('should use the inspect() property if `message` is not set', function () {
var err = {
err = {
showDiff: false,
inspect: function () { return 'an error happened'; }
};
var test = makeTest(err);
test = makeTest(err);
Base.list([test]);
var errOut = stdout.join('\n').trim();
errOut = stdout.join('\n').trim();
expect(errOut).to.equal('1) test title:\n an error happened');
});

it('should set an empty message if `message` and `inspect()` are not set', function () {
var err = {
err = {
showDiff: false
};
var test = makeTest(err);
test = makeTest(err);
Base.list([test]);
var errOut = stdout.join('\n').trim();
errOut = stdout.join('\n').trim();
expect(errOut).to.equal('1) test title:');
});

it('should not modify stack if it does not contain message', function () {
var err = {
err = {
message: 'Error',
stack: 'foo\nbar',
showDiff: false
};
var test = makeTest(err);
test = makeTest(err);

Base.list([test]);

var errOut = stdout.join('\n').trim();
errOut = stdout.join('\n').trim();
expect(errOut).to.equal('1) test title:\n Error\n foo\n bar');
});
});
Loading

0 comments on commit 9ba8eca

Please sign in to comment.