mocha
Version:
Classic, reliable, trusted test framework for Node.js and the browser
263 lines (220 loc) • 5.57 kB
JavaScript
/**
* @typedef {import('../runner.cjs')} Runner
*/
/**
* @module Nyan
*/
/**
* Module dependencies.
*/
import { Base } from "./base.js";
import Runner from "../runner.cjs";
var constants = Runner.constants;
var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
var EVENT_RUN_END = constants.EVENT_RUN_END;
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
class NyanCat extends Base {
static description = '"nyan cat"';
/**
* Constructs a new `Nyan` reporter instance.
*
* @public
* @memberof Mocha.reporters
* @extends Mocha.reporters.Base
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
constructor(runner, options) {
super(runner, options);
var self = this;
var width = (Base.window.width * 0.75) | 0;
var nyanCatWidth = (this.nyanCatWidth = 11);
this.colorIndex = 0;
this.numberOfLines = 4;
this.rainbowColors = self.generateColors();
this.scoreboardWidth = 5;
this.tick = 0;
this.trajectories = [[], [], [], []];
this.trajectoryWidthMax = width - nyanCatWidth;
runner.on(EVENT_RUN_BEGIN, function () {
Base.cursor.hide();
self.draw();
});
runner.on(EVENT_TEST_PENDING, function () {
self.draw();
});
runner.on(EVENT_TEST_PASS, function () {
self.draw();
});
runner.on(EVENT_TEST_FAIL, function () {
self.draw();
});
runner.once(EVENT_RUN_END, function () {
Base.cursor.show();
for (var i = 0; i < self.numberOfLines; i++) {
process.stdout.write("\n");
}
self.epilogue();
});
}
/**
* Draw the nyan cat
*
* @private
*/
draw() {
this.appendRainbow();
this.drawScoreboard();
this.drawRainbow();
this.drawNyanCat();
this.tick = !this.tick;
}
/**
* Draw the "scoreboard" showing the number
* of passes, failures and pending tests.
*
* @private
*/
drawScoreboard() {
var stats = this.stats;
function draw(type, n) {
process.stdout.write(" ");
process.stdout.write(Base.color(type, n));
process.stdout.write("\n");
}
draw("green", stats.passes);
draw("fail", stats.failures);
draw("pending", stats.pending);
process.stdout.write("\n");
this.cursorUp(this.numberOfLines);
}
/**
* Append the rainbow.
*
* @private
*/
appendRainbow() {
var segment = this.tick ? "_" : "-";
var rainbowified = this.rainbowify(segment);
for (var index = 0; index < this.numberOfLines; index++) {
var trajectory = this.trajectories[index];
if (trajectory.length >= this.trajectoryWidthMax) {
trajectory.shift();
}
trajectory.push(rainbowified);
}
}
/**
* Draw the rainbow.
*
* @private
*/
drawRainbow() {
var self = this;
this.trajectories.forEach(function (line) {
process.stdout.write("\u001b[" + self.scoreboardWidth + "C");
process.stdout.write(line.join(""));
process.stdout.write("\n");
});
this.cursorUp(this.numberOfLines);
}
/**
* Draw the nyan cat
*
* @private
*/
drawNyanCat() {
var self = this;
var startWidth = this.scoreboardWidth + this.trajectories[0].length;
var dist = "\u001b[" + startWidth + "C";
var padding;
process.stdout.write(dist);
process.stdout.write("_,------,");
process.stdout.write("\n");
process.stdout.write(dist);
padding = self.tick ? " " : " ";
process.stdout.write("_|" + padding + "/\\_/\\ ");
process.stdout.write("\n");
process.stdout.write(dist);
padding = self.tick ? "_" : "__";
var tail = self.tick ? "~" : "^";
process.stdout.write(tail + "|" + padding + this.face() + " ");
process.stdout.write("\n");
process.stdout.write(dist);
padding = self.tick ? " " : " ";
process.stdout.write(padding + '"" "" ');
process.stdout.write("\n");
this.cursorUp(this.numberOfLines);
}
/**
* Draw nyan cat face.
*
* @private
* @return {string}
*/
face() {
var stats = this.stats;
if (stats.failures) {
return "( x .x)";
} else if (stats.pending) {
return "( o .o)";
} else if (stats.passes) {
return "( ^ .^)";
}
return "( - .-)";
}
/**
* Move cursor up `n`.
*
* @private
* @param {number} n
*/
cursorUp(n) {
process.stdout.write("\u001b[" + n + "A");
}
/**
* Move cursor down `n`.
*
* @private
* @param {number} n
*/
cursorDown(n) {
process.stdout.write("\u001b[" + n + "B");
}
/**
* Generate rainbow colors.
*
* @private
* @return {Array}
*/
generateColors() {
var colors = [];
for (var i = 0; i < 6 * 7; i++) {
var pi3 = Math.floor(Math.PI / 3);
var n = i * (1.0 / 6);
var r = Math.floor(3 * Math.sin(n) + 3);
var g = Math.floor(3 * Math.sin(n + 2 * pi3) + 3);
var b = Math.floor(3 * Math.sin(n + 4 * pi3) + 3);
colors.push(36 * r + 6 * g + b + 16);
}
return colors;
}
/**
* Apply rainbow to the given `str`.
*
* @private
* @param {string} str
* @return {string}
*/
rainbowify(str) {
if (!Base.useColors) {
return str;
}
var color = this.rainbowColors[this.colorIndex % this.rainbowColors.length];
this.colorIndex += 1;
return "\u001b[38;5;" + color + "m" + str + "\u001b[0m";
}
}
export { NyanCat };