| 1 | "use strict"; |
| 2 | |
| 3 | /** |
| 4 | * @typedef {import('../runner.js')} Runner |
| 5 | * @typedef {import('../test.js')} Test |
| 6 | * @typedef {import('../types.d.ts').FullErrorStack} FullErrorStack |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * @module Base |
| 11 | */ |
| 12 | /** |
| 13 | * Module dependencies. |
| 14 | */ |
| 15 | |
| 16 | var diff = require("diff"); |
| 17 | var milliseconds = require("ms"); |
| 18 | var utils = require("../utils"); |
| 19 | var supportsColor = require("supports-color"); |
| 20 | var symbols = require("log-symbols"); |
| 21 | var constants = require("../runner").constants; |
| 22 | var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; |
| 23 | var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; |
| 24 | |
| 25 | const isBrowser = utils.isBrowser(); |
| 26 | |
| 27 | function getBrowserWindowSize() { |
| 28 | if ("innerHeight" in global) { |
| 29 | return [global.innerHeight, global.innerWidth]; |
| 30 | } |
| 31 | // In a Web Worker, the DOM Window is not available. |
| 32 | return [640, 480]; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Expose `Base`. |
| 37 | */ |
| 38 | |
| 39 | exports = module.exports = Base; |
| 40 | |
| 41 | /** |
| 42 | * Check if both stdio streams are associated with a tty. |
| 43 | */ |
| 44 | |
| 45 | var isatty = isBrowser || (process.stdout.isTTY && process.stderr.isTTY); |
| 46 | |
| 47 | /** |
| 48 | * Save log references to avoid tests interfering (see GH-3604). |
| 49 | */ |
| 50 | var consoleLog = console.log; |
| 51 | |
| 52 | /** |
| 53 | * Enable coloring by default, except in the browser interface. |
| 54 | */ |
| 55 | |
| 56 | exports.useColors = |
| 57 | !isBrowser && |
| 58 | (supportsColor.stdout || process.env.MOCHA_COLORS !== undefined); |
| 59 | |
| 60 | /** |
| 61 | * Inline diffs instead of +/- |
| 62 | */ |
| 63 | |
| 64 | exports.inlineDiffs = false; |
| 65 | |
| 66 | /** |
| 67 | * Truncate diffs longer than this value to avoid slow performance |
| 68 | */ |
| 69 | exports.maxDiffSize = 8192; |
| 70 | |
| 71 | /** |
| 72 | * Default color map. |
| 73 | */ |
| 74 | |
| 75 | exports.colors = { |
| 76 | pass: 90, |
| 77 | fail: 31, |
| 78 | "bright pass": 92, |
| 79 | "bright fail": 91, |
| 80 | "bright yellow": 93, |
| 81 | pending: 36, |
| 82 | suite: 0, |
| 83 | "error title": 0, |
| 84 | "error message": 31, |
| 85 | "error stack": 90, |
| 86 | checkmark: 32, |
| 87 | fast: 90, |
| 88 | medium: 33, |
| 89 | slow: 31, |
| 90 | green: 32, |
| 91 | light: 90, |
| 92 | "diff gutter": 90, |
| 93 | "diff added": 32, |
| 94 | "diff removed": 31, |
| 95 | "diff added inline": "30;42", |
| 96 | "diff removed inline": "30;41", |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * Default symbol map. |
| 101 | */ |
| 102 | |
| 103 | exports.symbols = { |
| 104 | ok: symbols.success, |
| 105 | err: symbols.error, |
| 106 | dot: ".", |
| 107 | comma: ",", |
| 108 | bang: "!", |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * Color `str` with the given `type`, |
| 113 | * allowing colors to be disabled, |
| 114 | * as well as user-defined color |
| 115 | * schemes. |
| 116 | * |
| 117 | * @private |
| 118 | * @param {string} type |
| 119 | * @param {string} str |
| 120 | * @return {string} |
| 121 | */ |
| 122 | var color = (exports.color = function (type, str) { |
| 123 | if (!exports.useColors) { |
| 124 | return String(str); |
| 125 | } |
| 126 | return "\u001b[" + exports.colors[type] + "m" + str + "\u001b[0m"; |
| 127 | }); |
| 128 | |
| 129 | /** |
| 130 | * Expose term window size, with some defaults for when stderr is not a tty. |
| 131 | */ |
| 132 | |
| 133 | exports.window = { |
| 134 | width: 75, |
| 135 | }; |
| 136 | |
| 137 | if (isatty) { |
| 138 | if (isBrowser) { |
| 139 | exports.window.width = getBrowserWindowSize()[1]; |
| 140 | } else { |
| 141 | exports.window.width = process.stdout.getWindowSize(1)[0]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Expose some basic cursor interactions that are common among reporters. |
| 147 | */ |
| 148 | |
| 149 | exports.cursor = { |
| 150 | hide: function () { |
| 151 | isatty && process.stdout.write("\u001b[?25l"); |
| 152 | }, |
| 153 | |
| 154 | show: function () { |
| 155 | isatty && process.stdout.write("\u001b[?25h"); |
| 156 | }, |
| 157 | |
| 158 | deleteLine: function () { |
| 159 | isatty && process.stdout.write("\u001b[2K"); |
| 160 | }, |
| 161 | |
| 162 | beginningOfLine: function () { |
| 163 | isatty && process.stdout.write("\u001b[0G"); |
| 164 | }, |
| 165 | |
| 166 | CR: function () { |
| 167 | if (isatty) { |
| 168 | exports.cursor.deleteLine(); |
| 169 | exports.cursor.beginningOfLine(); |
| 170 | } else { |
| 171 | process.stdout.write("\r"); |
| 172 | } |
| 173 | }, |
| 174 | }; |
| 175 | |
| 176 | var showDiff = (exports.showDiff = function (err) { |
| 177 | return ( |
| 178 | err && |
| 179 | err.showDiff !== false && |
| 180 | sameType(err.actual, err.expected) && |
| 181 | err.expected !== undefined |
| 182 | ); |
| 183 | }); |
| 184 | |
| 185 | function stringifyDiffObjs(err) { |
| 186 | if (!utils.isString(err.actual) || !utils.isString(err.expected)) { |
| 187 | err.actual = utils.stringify(err.actual); |
| 188 | err.expected = utils.stringify(err.expected); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns a diff between 2 strings with coloured ANSI output. |
| 194 | * |
| 195 | * @description |
| 196 | * The diff will be either inline or unified dependent on the value |
| 197 | * of `Base.inlineDiff`. |
| 198 | * |
| 199 | * @param {string} actual |
| 200 | * @param {string} expected |
| 201 | * @return {string} Diff |
| 202 | */ |
| 203 | |
| 204 | var generateDiff = (exports.generateDiff = function (actual, expected) { |
| 205 | try { |
| 206 | var maxLen = exports.maxDiffSize; |
| 207 | var skipped = 0; |
| 208 | if (maxLen > 0) { |
| 209 | skipped = Math.max(actual.length - maxLen, expected.length - maxLen); |
| 210 | actual = actual.slice(0, maxLen); |
| 211 | expected = expected.slice(0, maxLen); |
| 212 | } |
| 213 | let result = exports.inlineDiffs |
| 214 | ? inlineDiff(actual, expected) |
| 215 | : unifiedDiff(actual, expected); |
| 216 | if (skipped > 0) { |
| 217 | result = `${result}\n [mocha] output truncated to ${maxLen} characters, see "maxDiffSize" reporter-option\n`; |
| 218 | } |
| 219 | return result; |
| 220 | } catch (err) { |
| 221 | var msg = |
| 222 | "\n " + |
| 223 | color("diff added", "+ expected") + |
| 224 | " " + |
| 225 | color("diff removed", "- actual: failed to generate Mocha diff") + |
| 226 | "\n"; |
| 227 | return msg; |
| 228 | } |
| 229 | }); |
| 230 | |
| 231 | /** |
| 232 | * Traverses err.cause and returns all stack traces |
| 233 | * |
| 234 | * @private |
| 235 | * @param {Error} err |
| 236 | * @param {Set<Error>} [seen] |
| 237 | * @return {FullErrorStack} |
| 238 | */ |
| 239 | var getFullErrorStack = function (err, seen) { |
| 240 | if (seen && seen.has(err)) { |
| 241 | return { message: "", msg: "<circular>", stack: "" }; |
| 242 | } |
| 243 | |
| 244 | var message; |
| 245 | |
| 246 | if (typeof err.inspect === "function") { |
| 247 | message = err.inspect() + ""; |
| 248 | } else if (err.message && typeof err.message.toString === "function") { |
| 249 | message = err.message + ""; |
| 250 | } else { |
| 251 | message = ""; |
| 252 | } |
| 253 | |
| 254 | var msg; |
| 255 | var stack = err.stack || message; |
| 256 | var index = message ? stack.indexOf(message) : -1; |
| 257 | |
| 258 | if (index === -1) { |
| 259 | msg = message; |
| 260 | } else { |
| 261 | index += message.length; |
| 262 | msg = stack.slice(0, index); |
| 263 | // remove msg from stack |
| 264 | stack = stack.slice(index + 1); |
| 265 | |
| 266 | if (err.cause) { |
| 267 | seen = seen || new Set(); |
| 268 | seen.add(err); |
| 269 | const causeStack = getFullErrorStack(err.cause, seen); |
| 270 | stack += |
| 271 | "\n Caused by: " + |
| 272 | causeStack.msg + |
| 273 | (causeStack.stack ? "\n" + causeStack.stack : ""); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return { |
| 278 | message, |
| 279 | msg, |
| 280 | stack, |
| 281 | }; |
| 282 | }; |
| 283 | |
| 284 | /** |
| 285 | * Outputs the given `failures` as a list. |
| 286 | * |
| 287 | * @public |
| 288 | * @memberof Mocha.reporters.Base |
| 289 | * @variation 1 |
| 290 | * @param {Object[]} failures - Each is Test instance with corresponding |
| 291 | * Error property |
| 292 | */ |
| 293 | exports.list = function (failures) { |
| 294 | var multipleErr, multipleTest; |
| 295 | Base.consoleLog(); |
| 296 | failures.forEach(function (test, i) { |
| 297 | // format |
| 298 | var fmt = |
| 299 | color("error title", " %s) %s:\n") + |
| 300 | color("error message", " %s") + |
| 301 | color("error stack", "\n%s\n"); |
| 302 | |
| 303 | // msg |
| 304 | var err; |
| 305 | if (test.err && test.err.multiple) { |
| 306 | if (multipleTest !== test) { |
| 307 | multipleTest = test; |
| 308 | multipleErr = [test.err].concat(test.err.multiple); |
| 309 | } |
| 310 | err = multipleErr.shift(); |
| 311 | } else { |
| 312 | err = test.err; |
| 313 | } |
| 314 | |
| 315 | var { message, msg, stack } = getFullErrorStack(err); |
| 316 | |
| 317 | // uncaught |
| 318 | if (err.uncaught) { |
| 319 | msg = "Uncaught " + msg; |
| 320 | } |
| 321 | // explicitly show diff |
| 322 | if (!exports.hideDiff && showDiff(err)) { |
| 323 | stringifyDiffObjs(err); |
| 324 | fmt = |
| 325 | color("error title", " %s) %s:\n%s") + color("error stack", "\n%s\n"); |
| 326 | var match = message.match(/^([^:]+): expected/); |
| 327 | msg = "\n " + color("error message", match ? match[1] : msg); |
| 328 | |
| 329 | msg += generateDiff(err.actual, err.expected); |
| 330 | } |
| 331 | |
| 332 | // indent stack trace |
| 333 | stack = stack.replace(/^/gm, " "); |
| 334 | |
| 335 | // indented test title |
| 336 | var testTitle = ""; |
| 337 | test.titlePath().forEach(function (str, index) { |
| 338 | if (index !== 0) { |
| 339 | testTitle += "\n "; |
| 340 | } |
| 341 | for (var i = 0; i < index; i++) { |
| 342 | testTitle += " "; |
| 343 | } |
| 344 | testTitle += str; |
| 345 | }); |
| 346 | |
| 347 | Base.consoleLog(fmt, i + 1, testTitle, msg, stack); |
| 348 | }); |
| 349 | }; |
| 350 | |
| 351 | /** |
| 352 | * Constructs a new `Base` reporter instance. |
| 353 | * |
| 354 | * @description |
| 355 | * All other reporters generally inherit from this reporter. |
| 356 | * |
| 357 | * @public |
| 358 | * @class |
| 359 | * @memberof Mocha.reporters |
| 360 | * @param {Runner} runner - Instance triggers reporter actions. |
| 361 | * @param {Object} [options] - runner options |
| 362 | */ |
| 363 | function Base(runner, options) { |
| 364 | var failures = (this.failures = []); |
| 365 | |
| 366 | if (!runner) { |
| 367 | throw new TypeError("Missing runner argument"); |
| 368 | } |
| 369 | this.options = options || {}; |
| 370 | this.runner = runner; |
| 371 | this.stats = runner.stats; // assigned so Reporters keep a closer reference |
| 372 | |
| 373 | var maxDiffSizeOpt = |
| 374 | this.options.reporterOption && this.options.reporterOption.maxDiffSize; |
| 375 | if (maxDiffSizeOpt !== undefined && !isNaN(Number(maxDiffSizeOpt))) { |
| 376 | exports.maxDiffSize = Number(maxDiffSizeOpt); |
| 377 | } |
| 378 | |
| 379 | runner.on(EVENT_TEST_PASS, function (test) { |
| 380 | if (test.duration > test.slow()) { |
| 381 | test.speed = "slow"; |
| 382 | } else if (test.duration > test.slow() / 2) { |
| 383 | test.speed = "medium"; |
| 384 | } else { |
| 385 | test.speed = "fast"; |
| 386 | } |
| 387 | }); |
| 388 | |
| 389 | runner.on(EVENT_TEST_FAIL, function (test, err) { |
| 390 | if (showDiff(err)) { |
| 391 | stringifyDiffObjs(err); |
| 392 | } |
| 393 | // more than one error per test |
| 394 | if (test.err && err instanceof Error) { |
| 395 | test.err.multiple = (test.err.multiple || []).concat(err); |
| 396 | } else { |
| 397 | test.err = err; |
| 398 | } |
| 399 | failures.push(test); |
| 400 | }); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Outputs common epilogue used by many of the bundled reporters. |
| 405 | * |
| 406 | * @public |
| 407 | * @memberof Mocha.reporters |
| 408 | */ |
| 409 | Base.prototype.epilogue = function () { |
| 410 | var stats = this.stats; |
| 411 | var fmt; |
| 412 | |
| 413 | Base.consoleLog(); |
| 414 | |
| 415 | // passes |
| 416 | fmt = |
| 417 | color("bright pass", " ") + |
| 418 | color("green", " %d passing") + |
| 419 | color("light", " (%s)"); |
| 420 | |
| 421 | Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration)); |
| 422 | |
| 423 | // pending |
| 424 | if (stats.pending) { |
| 425 | fmt = color("pending", " ") + color("pending", " %d pending"); |
| 426 | |
| 427 | Base.consoleLog(fmt, stats.pending); |
| 428 | } |
| 429 | |
| 430 | // failures |
| 431 | if (stats.failures) { |
| 432 | fmt = color("fail", " %d failing"); |
| 433 | |
| 434 | Base.consoleLog(fmt, stats.failures); |
| 435 | |
| 436 | Base.list(this.failures); |
| 437 | Base.consoleLog(); |
| 438 | } |
| 439 | |
| 440 | Base.consoleLog(); |
| 441 | }; |
| 442 | |
| 443 | /** |
| 444 | * Pads the given `str` to `len`. |
| 445 | * |
| 446 | * @private |
| 447 | * @param {string} str |
| 448 | * @param {string} len |
| 449 | * @return {string} |
| 450 | */ |
| 451 | function pad(str, len) { |
| 452 | str = String(str); |
| 453 | return Array(len - str.length + 1).join(" ") + str; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Returns inline diff between 2 strings with coloured ANSI output. |
| 458 | * |
| 459 | * @private |
| 460 | * @param {String} actual |
| 461 | * @param {String} expected |
| 462 | * @return {string} Diff |
| 463 | */ |
| 464 | function inlineDiff(actual, expected) { |
| 465 | var msg = errorDiff(actual, expected); |
| 466 | |
| 467 | // linenos |
| 468 | var lines = msg.split("\n"); |
| 469 | if (lines.length > 4) { |
| 470 | var width = String(lines.length).length; |
| 471 | msg = lines |
| 472 | .map(function (str, i) { |
| 473 | return pad(++i, width) + " |" + " " + str; |
| 474 | }) |
| 475 | .join("\n"); |
| 476 | } |
| 477 | |
| 478 | // legend |
| 479 | msg = |
| 480 | "\n" + |
| 481 | color("diff removed inline", "actual") + |
| 482 | " " + |
| 483 | color("diff added inline", "expected") + |
| 484 | "\n\n" + |
| 485 | msg + |
| 486 | "\n"; |
| 487 | |
| 488 | // indent |
| 489 | msg = msg.replace(/^/gm, " "); |
| 490 | return msg; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Returns unified diff between two strings with coloured ANSI output. |
| 495 | * |
| 496 | * @private |
| 497 | * @param {String} actual |
| 498 | * @param {String} expected |
| 499 | * @return {string} The diff. |
| 500 | */ |
| 501 | function unifiedDiff(actual, expected) { |
| 502 | var indent = " "; |
| 503 | function cleanUp(line) { |
| 504 | if (line[0] === "+") { |
| 505 | return indent + colorLines("diff added", line); |
| 506 | } |
| 507 | if (line[0] === "-") { |
| 508 | return indent + colorLines("diff removed", line); |
| 509 | } |
| 510 | if (line.match(/@@/)) { |
| 511 | return "--"; |
| 512 | } |
| 513 | if (line.match(/\\ No newline/)) { |
| 514 | return null; |
| 515 | } |
| 516 | return indent + line; |
| 517 | } |
| 518 | function notBlank(line) { |
| 519 | return typeof line !== "undefined" && line !== null; |
| 520 | } |
| 521 | var msg = diff.createPatch("string", actual, expected); |
| 522 | var lines = msg.split("\n").splice(5); |
| 523 | return ( |
| 524 | "\n " + |
| 525 | colorLines("diff added", "+ expected") + |
| 526 | " " + |
| 527 | colorLines("diff removed", "- actual") + |
| 528 | "\n\n" + |
| 529 | lines.map(cleanUp).filter(notBlank).join("\n") |
| 530 | ); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Returns character diff for `err`. |
| 535 | * |
| 536 | * @private |
| 537 | * @param {String} actual |
| 538 | * @param {String} expected |
| 539 | * @return {string} the diff |
| 540 | */ |
| 541 | function errorDiff(actual, expected) { |
| 542 | return diff |
| 543 | .diffWordsWithSpace(actual, expected) |
| 544 | .map(function (str) { |
| 545 | if (str.added) { |
| 546 | return colorLines("diff added inline", str.value); |
| 547 | } |
| 548 | if (str.removed) { |
| 549 | return colorLines("diff removed inline", str.value); |
| 550 | } |
| 551 | return str.value; |
| 552 | }) |
| 553 | .join(""); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Colors lines for `str`, using the color `name`. |
| 558 | * |
| 559 | * @private |
| 560 | * @param {string} name |
| 561 | * @param {string} str |
| 562 | * @return {string} |
| 563 | */ |
| 564 | function colorLines(name, str) { |
| 565 | return str |
| 566 | .split("\n") |
| 567 | .map(function (str) { |
| 568 | return color(name, str); |
| 569 | }) |
| 570 | .join("\n"); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Object#toString reference. |
| 575 | */ |
| 576 | var objToString = Object.prototype.toString; |
| 577 | |
| 578 | /** |
| 579 | * Checks that a / b have the same type. |
| 580 | * |
| 581 | * @private |
| 582 | * @param {Object} a |
| 583 | * @param {Object} b |
| 584 | * @return {boolean} |
| 585 | */ |
| 586 | function sameType(a, b) { |
| 587 | return objToString.call(a) === objToString.call(b); |
| 588 | } |
| 589 | |
| 590 | Base.consoleLog = consoleLog; |
| 591 | |
| 592 | Base.abstract = true; |