Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sinon sandbox for reporter tests #3888

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
});
Loading