Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

improve: dry up reporter/base test

Maximilian Antonif8e2b0fdd145
diff --git a/test/reporters/base.js b/test/reporters/base.js
index d8ca2c7b..cb8a6a67 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/);
});
+
});