Skip to content

Commit

Permalink
Use sinon sandbox for reporter tests (#3888)
Browse files Browse the repository at this point in the history
  • Loading branch information
outsideris authored and plroebuck committed Apr 22, 2019
1 parent 59fcf71 commit 57a9738
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 216 deletions.
55 changes: 17 additions & 38 deletions test/reporters/landing.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var sandbox = require('sinon').createSandbox();
var Mocha = require('../..');
var reporters = Mocha.reporters;
var Landing = reporters.Landing;
Expand All @@ -15,8 +16,6 @@ describe('Landing reporter', function() {
var runner;
var options = {};
var runReporter = makeRunReporter(Landing);
var useColors;
var windowWidth;
var resetCode = '\u001b[0m';
var expectedArray = [
'\u001b[1D\u001b[2A',
Expand All @@ -30,40 +29,32 @@ describe('Landing reporter', function() {
];

beforeEach(function() {
useColors = Base.useColors;
Base.useColors = false;
windowWidth = Base.window.width;
Base.window.width = 1;
sandbox.stub(Base, 'useColors').value(false);
sandbox.stub(Base.window, 'width').value(1);
});

afterEach(function() {
Base.useColors = useColors;
Base.window.width = windowWidth;
sandbox.restore();
runner = undefined;
});

describe('on start', function() {
it('should write new lines', function() {
var cachedCursor = Base.cursor;
Base.cursor.hide = function() {};
sandbox.stub(Base.cursor, 'hide');

runner = createMockRunner('start', 'start');
var stdout = runReporter({}, runner, options);

expect(stdout[0], 'to equal', '\n\n\n ');
Base.cursor = cachedCursor;
});

it('should call cursor hide', function() {
var cachedCursor = Base.cursor;
var calledCursorHide = false;
Base.cursor.hide = function() {
calledCursorHide = true;
};
sandbox.stub(Base.cursor, 'hide');

runner = createMockRunner('start', 'start');
runReporter({}, runner, options);
expect(calledCursorHide, 'to be', true);

Base.cursor = cachedCursor;
expect(Base.cursor.hide, 'was called');
});
});

Expand Down Expand Up @@ -95,28 +86,16 @@ describe('Landing reporter', function() {
});
describe('on end', function() {
it('should call cursor show and epilogue', function() {
var cachedCursor = Base.cursor;
var calledCursorShow = false;
Base.cursor.show = function() {
calledCursorShow = true;
};
var reporterStub = {epilogue: function() {}};
sandbox.stub(Base.cursor, 'show');
sandbox.stub(reporterStub, 'epilogue');

runner = createMockRunner('end', 'end');

var calledEpilogue = false;
runReporter(
{
epilogue: function() {
calledEpilogue = true;
}
},
runner,
options
);

expect(calledEpilogue, 'to be', true);
expect(calledCursorShow, 'to be', true);

Base.cursor = cachedCursor;
runReporter(reporterStub, runner, options);

expect(reporterStub.epilogue, 'was called');
expect(Base.cursor.show, 'was called');
});
});
});
61 changes: 19 additions & 42 deletions test/reporters/list.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var sandbox = require('sinon').createSandbox();
var reporters = require('../../').reporters;
var List = reporters.List;
var Base = reporters.Base;
Expand All @@ -11,7 +12,6 @@ describe('List reporter', function() {
var runner;
var options = {};
var runReporter = makeRunReporter(List);
var useColors;
var expectedTitle = 'some title';
var expectedDuration = 100;
var test = {
Expand All @@ -23,12 +23,11 @@ describe('List reporter', function() {
};

beforeEach(function() {
useColors = Base.useColors;
Base.useColors = false;
sandbox.stub(Base, 'useColors').value(false);
});

afterEach(function() {
Base.useColors = useColors;
sandbox.restore();
runner = undefined;
});

Expand All @@ -53,24 +52,18 @@ describe('List reporter', function() {
});
describe('on pass', function() {
it('should call cursor CR', function() {
var calledCursorCR = false;
var cachedCursor = Base.cursor;
Base.cursor.CR = function() {
calledCursorCR = true;
};
sandbox.stub(Base.cursor, 'CR');

runner = createMockRunner('pass', 'pass', null, null, test);
runReporter({epilogue: function() {}}, runner, options);

expect(calledCursorCR, 'to be', true);

Base.cursor = cachedCursor;
expect(Base.cursor.CR, 'was called');
});
it('should write expected symbol, title and duration to the console', function() {
var cachedSymbols = Base.symbols;
var expectedOkSymbol = 'OK';
Base.symbols.ok = expectedOkSymbol;
var cachedCursor = Base.cursor;
Base.cursor.CR = function() {};
sandbox.stub(Base.symbols, 'ok').value(expectedOkSymbol);
sandbox.stub(Base.cursor, 'CR');

runner = createMockRunner('pass', 'pass', null, null, test);
var stdout = runReporter({epilogue: function() {}}, runner, options);

Expand All @@ -85,29 +78,21 @@ describe('List reporter', function() {
expectedDuration +
'ms\n'
);

Base.cursor = cachedCursor;
Base.symbols = cachedSymbols;
});
});
describe('on fail', function() {
it('should call cursor CR', function() {
var calledCursorCR = false;
var cachedCursor = Base.cursor;
Base.cursor.CR = function() {
calledCursorCR = true;
};
sandbox.stub(Base.cursor, 'CR');

runner = createMockRunner('fail', 'fail', null, null, test);
runReporter({epilogue: function() {}}, runner, options);

expect(calledCursorCR, 'to be', true);

Base.cursor = cachedCursor;
expect(Base.cursor.CR, 'was called');
});
it('should write expected error number and title', function() {
var cachedCursor = Base.cursor;
sandbox.stub(Base.cursor, 'CR');

var expectedErrorCount = 1;
Base.cursor.CR = function() {};
runner = createMockRunner('fail', 'fail', null, null, test);
var stdout = runReporter({epilogue: function() {}}, runner, options);

Expand All @@ -116,8 +101,6 @@ describe('List reporter', function() {
'to be',
' ' + expectedErrorCount + ') ' + expectedTitle + '\n'
);

Base.cursor = cachedCursor;
});
it('should immediately construct fail strings', function() {
var actual = {a: 'actual'};
Expand Down Expand Up @@ -149,19 +132,13 @@ describe('List reporter', function() {

describe('on end', function() {
it('should call epilogue', function() {
var calledEpilogue = false;
var reporterStub = {epilogue: function() {}};
sandbox.stub(reporterStub, 'epilogue');

runner = createMockRunner('end', 'end');
runReporter(
{
epilogue: function() {
calledEpilogue = true;
}
},
runner,
options
);
runReporter(reporterStub, runner, options);

expect(calledEpilogue, 'to be', true);
expect(reporterStub.epilogue, 'was called');
});
});
});

0 comments on commit 57a9738

Please sign in to comment.