Skip to content

armgit / mocha

Public Imported from GitHub
0

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

100% coverage for dot and spec reporters

Craig Taub267ff661e093
diff --git a/test/reporters/dot.spec.js b/test/reporters/dot.spec.js
new file mode 100644
index 00000000..2764336a
--- /dev/null
+++ b/test/reporters/dot.spec.js
@@ -0,0 +1,135 @@
+'use strict';
+
+var reporters = require('../../').reporters;
+var Dot = reporters.Dot;
+
+describe('Dot reporter', function () {
+ var stdout;
+ var stdoutWrite;
+ var runner;
+ var ansiColorCodeReset = '\u001b[0m';
+ var ansiColorCodeCyan = '\u001b[36m';
+ var ansiColorCodeGrey = '\u001b[90m';
+ var ansiColorCodeBrightYellow = '\u001b[93m';
+ var ansiColorCodeRed = '\u001b[31m';
+
+ beforeEach(function () {
+ stdout = [];
+ runner = {};
+ stdoutWrite = process.stdout.write;
+ process.stdout.write = function (string) {
+ stdout.push(string);
+ };
+ });
+
+ describe('on start', function () {
+ it('should return a new line', function () {
+ runner.on = function (event, callback) {
+ if (event === 'start') {
+ callback();
+ }
+ };
+ Dot.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ '\n'
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+ describe('on pending', function () {
+ it('should return a new line and a coma in the color cyan', function () {
+ runner.on = function (event, callback) {
+ if (event === 'pending') {
+ callback();
+ }
+ };
+ Dot.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ '\n ',
+ ansiColorCodeCyan + ',' + ansiColorCodeReset
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+ describe('on pass', function () {
+ describe('if test speed is fast', function () {
+ it('should return a new line and a dot in the color grey', function () {
+ var test = {
+ duration: 1,
+ slow: function () { return 2; }
+ };
+ runner.on = function (event, callback) {
+ if (event === 'pass') {
+ callback(test);
+ }
+ };
+ Dot.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ '\n ',
+ ansiColorCodeGrey + '․' + ansiColorCodeReset
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+ describe('if test speed is slow', function () {
+ it('should return a new line and a dot in the color bright yellow', function () {
+ var test = {
+ duration: 2,
+ slow: function () { return 1; }
+ };
+ runner.on = function (event, callback) {
+ if (event === 'pass') {
+ callback(test);
+ }
+ };
+ Dot.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ '\n ',
+ ansiColorCodeBrightYellow + '․' + ansiColorCodeReset
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+ });
+ describe('on fail', function () {
+ it('should return a new line and a exclamation mark in the color red', function () {
+ var test = {
+ test: {
+ err: 'some error'
+ }
+ };
+ runner.on = function (event, callback) {
+ if (event === 'fail') {
+ callback(test);
+ }
+ };
+ Dot.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ '\n ',
+ ansiColorCodeRed + '!' + ansiColorCodeReset
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+ describe('on end', function () {
+ it('should call the epilogue', function () {
+ runner.on = function (event, callback) {
+ if (event === 'end') {
+ callback();
+ }
+ };
+ var epilogueCalled = false;
+ var epilogue = function () {
+ epilogueCalled = true;
+ };
+ Dot.call({epilogue: epilogue}, runner);
+ process.stdout.write = stdoutWrite;
+ epilogueCalled.should.be.true();
+ });
+ });
+});
diff --git a/test/reporters/spec.spec.js b/test/reporters/spec.spec.js
new file mode 100644
index 00000000..4c2dd7af
--- /dev/null
+++ b/test/reporters/spec.spec.js
@@ -0,0 +1,124 @@
+'use strict';
+
+var reporters = require('../../').reporters;
+var Spec = reporters.Spec;
+
+describe('Spec reporter', function () {
+ var stdout;
+ var stdoutWrite;
+ var runner;
+ var ansiColorCodeReset = '\u001b[0m';
+ var ansiColorCodeCyan = '\u001b[36m';
+ var ansiColorCodeRed = '\u001b[31m';
+ var ansiColorCodeGreen = '\u001b[32m';
+ var ansiColorCodeGrey = '\u001b[90m';
+
+ beforeEach(function () {
+ stdout = [];
+ runner = {};
+ stdoutWrite = process.stdout.write;
+ process.stdout.write = function (string) {
+ stdout.push(string);
+ };
+ });
+
+ describe('on suite', function () {
+ it('should log title indented in color', function () {
+ var expectedTitle = 'expectedTitle';
+ var suite = {
+ title: expectedTitle
+ };
+ runner.on = function (event, callback) {
+ if (event === 'suite') {
+ callback(suite);
+ }
+ };
+ Spec.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ ansiColorCodeReset + expectedTitle + ansiColorCodeReset + '\n'
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+ describe('on pending', function () {
+ it('should log title indented in the color cyan', function () {
+ var expectedTitle = 'expectedTitle';
+ var suite = {
+ title: expectedTitle
+ };
+ runner.on = function (event, callback) {
+ if (event === 'pending') {
+ callback(suite);
+ }
+ };
+ Spec.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ ansiColorCodeCyan + ' - ' + expectedTitle + ansiColorCodeReset + '\n'
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+ describe('on pass', function () {
+ describe('if test speed is slow', function () {
+ it('should return expected green tick, grey title and with duration colored in red', function () {
+ var expectedTitle = 'expectedTitle';
+ var expectedDuration = 2;
+ var test = {
+ title: expectedTitle,
+ duration: expectedDuration,
+ slow: function () { return 1; }
+ };
+ runner.on = function (event, callback) {
+ if (event === 'pass') {
+ callback(test);
+ }
+ };
+ Spec.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedString = ansiColorCodeGreen + ' ✓' + ansiColorCodeReset + ansiColorCodeGrey + ' ' + expectedTitle + ansiColorCodeReset + ansiColorCodeRed + ' (' + expectedDuration + 'ms)' + ansiColorCodeReset + '\n';
+ stdout[0].should.equal(expectedString);
+ });
+ });
+ describe('if test speed is fast', function () {
+ it('should return expected green tick, grey title and without a duration', function () {
+ var expectedTitle = 'expectedTitle';
+ var expectedDuration = 1;
+ var test = {
+ title: expectedTitle,
+ duration: expectedDuration,
+ slow: function () { return 2; }
+ };
+ runner.on = function (event, callback) {
+ if (event === 'pass') {
+ callback(test);
+ }
+ };
+ Spec.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedString = ansiColorCodeGreen + ' ✓' + ansiColorCodeReset + ansiColorCodeGrey + ' ' + expectedTitle + ansiColorCodeReset + '\n';
+ stdout[0].should.equal(expectedString);
+ });
+ });
+ });
+ describe('on fail', function () {
+ it('should log title indented in the color red', function () {
+ var expectedTitle = 'expectedTitle';
+ var test = {
+ title: expectedTitle
+ };
+ runner.on = function (event, callback) {
+ if (event === 'fail') {
+ callback(test);
+ }
+ };
+ Spec.call({epilogue: function () {}}, runner);
+ process.stdout.write = stdoutWrite;
+ var expectedArray = [
+ ansiColorCodeRed + ' 1) ' + expectedTitle + ansiColorCodeReset + '\n'
+ ];
+ stdout.should.deepEqual(expectedArray);
+ });
+ });
+});