Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Use sinon sandbox for reporter tests (#3888)

JeongHoon Byun (aka Outsider)57a97386109b
diff --git a/test/reporters/landing.spec.js b/test/reporters/landing.spec.js
index e561c23d..dd73d9e5 100644
--- a/test/reporters/landing.spec.js
+++ b/test/reporters/landing.spec.js
@@ -1,5 +1,6 @@
'use strict';
+var sandbox = require('sinon').createSandbox();
var Mocha = require('../..');
var reporters = Mocha.reporters;
var Landing = reporters.Landing;
@@ -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',
@@ -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');
});
});
@@ -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');
});
});
});
diff --git a/test/reporters/list.spec.js b/test/reporters/list.spec.js
index 5abbe0bf..c9eceb87 100644
--- a/test/reporters/list.spec.js
+++ b/test/reporters/list.spec.js
@@ -1,5 +1,6 @@
'use strict';
+var sandbox = require('sinon').createSandbox();
var reporters = require('../../').reporters;
var List = reporters.List;
var Base = reporters.Base;
@@ -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 = {
@@ -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;
});
@@ -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);
@@ -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);
@@ -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'};
@@ -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');
});
});
});
diff --git a/test/reporters/nyan.spec.js b/test/reporters/nyan.spec.js
index 96bbda67..b1ad3a22 100644
--- a/test/reporters/nyan.spec.js
+++ b/test/reporters/nyan.spec.js
@@ -1,5 +1,6 @@
'use strict';
+var sandbox = require('sinon').createSandbox();
var reporters = require('../../').reporters;
var NyanCat = reporters.Nyan;
var Base = reporters.Base;
@@ -10,110 +11,90 @@ var makeRunReporter = require('./helpers.js').createRunReporterFunction;
describe('Nyan reporter', function() {
describe('events', function() {
var runner;
- var calledDraw;
var options = {};
var runReporter = makeRunReporter(NyanCat);
afterEach(function() {
+ sandbox.restore();
runner = undefined;
});
describe('on start', function() {
it('should call draw', function() {
- calledDraw = false;
+ var reporterStub = {
+ draw: function() {},
+ generateColors: function() {}
+ };
+ sandbox.stub(reporterStub, 'draw');
+
runner = createMockRunner('start', 'start');
- runReporter(
- {
- draw: function() {
- calledDraw = true;
- },
- generateColors: function() {}
- },
- runner,
- options
- );
+ runReporter(reporterStub, runner, options);
- expect(calledDraw, 'to be', true);
+ expect(reporterStub.draw, 'was called');
});
});
describe('on pending', function() {
it('should call draw', function() {
- calledDraw = false;
+ var reporterStub = {
+ draw: function() {},
+ generateColors: function() {}
+ };
+ sandbox.stub(reporterStub, 'draw');
+
runner = createMockRunner('pending', 'pending');
- runReporter(
- {
- draw: function() {
- calledDraw = true;
- },
- generateColors: function() {}
- },
- runner,
- options
- );
+ runReporter(reporterStub, runner, options);
- expect(calledDraw, 'to be', true);
+ expect(reporterStub.draw, 'was called');
});
});
describe('on pass', function() {
it('should call draw', function() {
- calledDraw = false;
+ var reporterStub = {
+ draw: function() {},
+ generateColors: function() {}
+ };
+ sandbox.stub(reporterStub, 'draw');
+
var test = {
duration: '',
slow: function() {}
};
runner = createMockRunner('pass', 'pass', null, null, test);
- runReporter(
- {
- draw: function() {
- calledDraw = true;
- },
- generateColors: function() {}
- },
- runner,
- options
- );
+ runReporter(reporterStub, runner, options);
- expect(calledDraw, 'to be', true);
+ expect(reporterStub.draw, 'was called');
});
});
describe('on fail', function() {
it('should call draw', function() {
- calledDraw = false;
+ var reporterStub = {
+ draw: function() {},
+ generateColors: function() {}
+ };
+ sandbox.stub(reporterStub, 'draw');
+
var test = {
err: ''
};
runner = createMockRunner('fail', 'fail', null, null, test);
- runReporter(
- {
- draw: function() {
- calledDraw = true;
- },
- generateColors: function() {}
- },
- runner,
- options
- );
+ runReporter(reporterStub, runner, options);
- expect(calledDraw, 'to be', true);
+ expect(reporterStub.draw, 'was called');
});
});
describe('on end', function() {
it('should call epilogue', function() {
- var calledEpilogue = false;
+ var reporterStub = {
+ draw: function() {},
+ generateColors: function() {},
+ epilogue: function() {}
+ };
+ sandbox.stub(reporterStub, 'epilogue');
+
runner = createMockRunner('end', 'end');
- runReporter(
- {
- draw: function() {},
- generateColors: function() {},
- epilogue: function() {
- calledEpilogue = true;
- }
- },
- runner,
- options
- );
+ runReporter(reporterStub, runner, options);
- expect(calledEpilogue, 'to be', true);
+ expect(reporterStub.epilogue, 'was called');
});
it('should write numberOfLines amount of new lines', function() {
var expectedNumberOfLines = 4;
@@ -135,11 +116,8 @@ describe('Nyan reporter', function() {
expect(arrayOfNewlines, 'to have length', expectedNumberOfLines);
});
it('should call Base show', function() {
- var showCalled = false;
- var cachedShow = Base.cursor.show;
- Base.cursor.show = function() {
- showCalled = true;
- };
+ sandbox.stub(Base.cursor, 'show');
+
runner = createMockRunner('end', 'end');
runReporter(
{
@@ -151,8 +129,7 @@ describe('Nyan reporter', function() {
options
);
- expect(showCalled, 'to be', true);
- Base.cursor.show = cachedShow;
+ expect(Base.cursor.show, 'was called');
});
});
});
@@ -299,15 +276,8 @@ describe('Nyan reporter', function() {
describe('rainbowify', function() {
describe('useColors is false', function() {
- var useColors;
-
beforeEach(function() {
- useColors = Base.useColors;
- Base.useColors = false;
- });
-
- afterEach(function() {
- Base.useColors = useColors;
+ sandbox.stub(Base, 'useColors').value(false);
});
it('should return argument string', function() {
@@ -319,16 +289,10 @@ describe('Nyan reporter', function() {
});
});
describe('useColors is true', function() {
- var useColors;
-
beforeEach(function() {
- useColors = Base.useColors;
- Base.useColors = true;
+ sandbox.stub(Base, 'useColors').value(true);
});
- afterEach(function() {
- Base.useColors = useColors;
- });
it('should return rainbowified string from the given string and predefined codes', function() {
var startCode = '\u001b[38;5;';
var endCode = '\u001b[0m';
diff --git a/test/reporters/progress.spec.js b/test/reporters/progress.spec.js
index f19bb398..cdea9c64 100644
--- a/test/reporters/progress.spec.js
+++ b/test/reporters/progress.spec.js
@@ -1,5 +1,6 @@
'use strict';
+var sandbox = require('sinon').createSandbox();
var reporters = require('../../').reporters;
var Progress = reporters.Progress;
var Base = reporters.Base;
@@ -23,34 +24,27 @@ describe('Progress reporter', function() {
});
afterEach(function() {
+ sandbox.restore();
process.stdout.write = stdoutWrite;
});
describe('on start', function() {
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, {});
- expect(calledCursorHide, 'to be', true);
-
- Base.cursor = cachedCursor;
+ expect(Base.cursor.hide, 'was called');
});
});
describe('on test end', function() {
describe('if line has not changed', function() {
it('should return and not write anything', function() {
- var cachedCursor = Base.cursor;
- var useColors = Base.useColors;
- Base.useColors = false;
- Base.cursor.CR = function() {};
- var windowWidth = Base.window.width;
- Base.window.width = -3;
+ sandbox.stub(Base, 'useColors').value(false);
+ sandbox.stub(Base.cursor, 'CR');
+ sandbox.stub(Base.window, 'width').value(-3);
var expectedTotal = 1;
var expectedOptions = {};
@@ -59,23 +53,13 @@ describe('Progress reporter', function() {
var stdout = runReporter({}, runner, expectedOptions);
expect(stdout, 'to equal', []);
-
- Base.cursor = cachedCursor;
- Base.useColors = useColors;
- Base.window.width = windowWidth;
});
});
describe('if line has changed', function() {
it('should write expected progress of open and close options', function() {
- var calledCursorCR = false;
- var cachedCursor = Base.cursor;
- var useColors = Base.useColors;
- Base.useColors = false;
- Base.cursor.CR = function() {
- calledCursorCR = true;
- };
- var windowWidth = Base.window.width;
- Base.window.width = 5;
+ sandbox.stub(Base, 'useColors').value(false);
+ sandbox.stub(Base.cursor, 'CR');
+ sandbox.stub(Base.window, 'width').value(5);
var expectedTotal = 12;
var expectedOpen = 'OpEn';
@@ -101,39 +85,23 @@ describe('Progress reporter', function() {
expectedIncomplete,
expectedClose
];
- expect(calledCursorCR, 'to be', true);
+ expect(Base.cursor.CR, 'was called');
expect(stdout, 'to equal', expectedArray);
-
- Base.cursor = cachedCursor;
- Base.useColors = useColors;
- Base.window.width = windowWidth;
});
});
});
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,
- {}
- );
-
- expect(calledEpilogue, 'to be', true);
- expect(calledCursorShow, 'to be', true);
-
- Base.cursor = cachedCursor;
+ runReporter(reporterStub, runner, {});
+
+ expect(reporterStub.epilogue, 'was called');
+ expect(Base.cursor.show, 'was called');
});
});
});