All files / lib/reporter stdout.js

100% Statements 112/112
100% Branches 52/52
100% Functions 21/21
100% Lines 100/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276              2x 2x 2x   2x 2x 2x 2x 2x   2x 2x 2x     2x           783x           2x 1012x 1012x             2x 3x   3x 209x 1x   209x 207x   209x 1x       3x 3x   3x 3x             2x   5x   5x 3x 3x       5x 2x 2x       211x 5x 4x 4x     5x 211x 211x   5x 4x 4x 4x               2x   2x 2x 2x   2x   2x 3x 3x 1x   3x       2x               2x 2x                   2x 2x 2x 2x 2x                   5x 5x 4x 4x                   4x 4x                   29x 29x 28x 28x                   28x 28x                   208x 208x 207x 207x                   207x 207x                   533x 533x 533x                   2x 2x 2x                   5x 5x 5x 5x   5x   2x 1x   1x   2x                 2x    
"use strict";
/**
 * `GlaceJS` stdout reporter.
 *
 * @module
 */
 
const fs = require("fs");
const path = require("path");
const util = require("util");
 
const _ = require("lodash");
const colors = require("colors");
const fse = require("fs-extra");
const MochaReporter = require("mocha").reporters.base;
const prettyms = require("pretty-ms");
 
const CONF = require("../config");
const TestCase = require("../testing").TestCase;
const utils = require("../utils");
 
let report;
let indents = 0;
 
/**
 * Calculate message indentation.
 * @ignore
 */
const indent = () => Array(indents).join("  ");
 
/**
 * Write report to stdout.
 * @ignore
 */
const stdout = function() {
    report.write(colors.strip(util.format.apply(util, arguments)) + "\n");
    console.log.apply(console, arguments);
};
 
/**
 * Print epilogue results.
 * @ignore
 */
const epilogue = () => {
    const passedTests = [], failedTests = [], skippedTests = [];
 
    for (const testCase of CONF.test.cases) {
        if (testCase.status === TestCase.FAILED) {
            failedTests.push(testCase);
        }
        if (testCase.status === TestCase.PASSED) {
            passedTests.push(testCase);
        }
        if (testCase.status === TestCase.SKIPPED) {
            skippedTests.push(testCase);
        }
    }
 
    printStatistics(passedTests.length, failedTests.length);
    if (skippedTests.length) printSkippedTests(skippedTests);
 
    utils.printTestErrors(failedTests, stdout);
    utils.printSessionErrors(stdout);
};
 
/**
 * Prints tests statistics.
 * @ignore
 */
const printStatistics = (passedNum, failedNum) => {
    let msg;
    const indent = "  ";
 
    if (passedNum) {
        msg = "passed test" + (passedNum === 1 ? "" : "s");
        stdout((indent + MochaReporter.symbols.ok + " " +
                String(passedNum).bold + " " + msg).green);
    }
 
    if (failedNum) {
        msg = "failed test" + (failedNum === 1 ? "" : "s");
        stdout((indent + MochaReporter.symbols.err + " " +
                String(failedNum).bold + " " + msg).red);
    }
 
    const chunksNum = CONF.test.cases.reduce((a, b) => a + b.chunks.length, 0);
    if (chunksNum) {
        msg = "executed chunk" + (chunksNum === 1 ? "" : "s");
        stdout(indent + chunksNum + " " + msg);
    }
 
    let execTime = CONF.test.cases
        .map(t => t.duration)
        .reduce((a, b) => a + b, 0);
 
    if (execTime > 0) {
        execTime = (execTime < 60000 ? `${execTime / 1000} sec` : prettyms(execTime)).bold;
        stdout();
        stdout(indent + "Summary tests time is", execTime);
    }
};
 
/**
 * Prints skipped tests.
 * @ignore
 */
const printSkippedTests = skippedTests => {
    let msg;
    const indent = "  ";
    msg = "skipped test" + (skippedTests.length === 1 ? "" : "s");
    stdout();
    // HACK https://github.com/glacejs/glace-core/issues/136
    stdout(indent + "# ".gray + String(skippedTests.length).gray.bold + " " + msg.gray);
 
    for (const skip of skippedTests) {
        msg = `* '${skip.name}'`;
        if (skip.rawInfo[0]) {
            msg += " - " + skip.rawInfo[0].bold;
        }
        stdout(indent + indent + msg.gray);
    }
};
 
module.exports = {
    /**
     * Called before tests start.
     *
     * @method
     * @instance
     */
    start: () => {
        fse.mkdirsSync(CONF.report.dir);
        report = fs.createWriteStream(
            path.resolve(CONF.report.dir, "stdout.log"), { flags : "w" });
    },
    /**
     * Called before tests end.
     *
     * @method
     * @instance
     */
    end: () => {
        epilogue();
        stdout();
        const reportMsg = "Local report is " + CONF.report.dir;
        stdout(Array(reportMsg.length + 1).join("-").yellow);
        stdout(reportMsg.yellow);
    },
    /**
     * Called on scope start.
     *
     * @method
     * @instance
     * @arg {object} scope - `MochaJS` suite.
     */
    scope: scope => {
        ++indents;
        if (indents) {
            stdout();
            stdout((indent() + "scope: " + scope.title).cyan);
        }
    },
    /**
     * Called before scope end.
     *
     * @method
     * @instance
     */
    scopeEnd: () => {
        --indents;
        if (!indents) stdout();
    },
    /**
     * Called on suite start.
     *
     * @method
     * @instance
     * @arg {object} suite - `MochaJS` suite.
     */
    suite: suite => {
        ++indents;
        if (indents) {
            stdout();
            stdout((indent() + "suite: " + suite.title).cyan);
        }
    },
    /**
     * Called before suite end.
     *
     * @method
     * @instance
     */
    suiteEnd: () => {
        --indents;
        if (!indents) stdout();
    },
    /**
     * Called on test start.
     *
     * @method
     * @instance
     * @arg {object} test - `MochaJS` suite.
     */
    test: test => {
        ++indents;
        if (indents) {
            stdout();
            stdout((indent() + "test: " + test.title).cyan.bold);
        }
    },
    /**
     * Called on test end.
     *
     * @method
     * @instance
     */
    testEnd: () => {
        --indents;
        if (!indents) stdout();
    },
    /**
     * Called on chunk passed.
     *
     * @method
     * @instance
     * @arg {object} chunk - `MochaJS` test.
     */
    pass: chunk => {
        let msg = indent() + "  " + MochaReporter.symbols.ok + " chunk";
        if (chunk.title) msg += ": " + chunk.title;
        stdout(msg.green);
    },
    /**
     * Called on chunk skipped.
     *
     * @method
     * @instance
     * @arg {object} chunk - `MochaJS` test.
     */
    skip: chunk => {
        let msg = indent() + "  # chunk";
        if (chunk.title) msg += ": " + chunk.title;
        stdout(msg.gray);
    },
    /**
     * Called on chunk or hook failed.
     *
     * @method
     * @instance
     * @arg {object} chunk - `MochaJS` test.
     */
    fail: chunk => {
        let suffix = chunk.type === "hook" ? " hook" : " chunk";
        let msg = indent() + "  " + MochaReporter.symbols.err + suffix;
        if (chunk.title) msg += ": " + chunk.title;
        stdout(msg.red);
 
        if (CONF.report.errorsNow) {
            let errMsg;
            if (CONF.test.curCase) {
                errMsg = _.last(CONF.test.curCase.errors);
            } else {
                errMsg = _.last(CONF.session.errors);
            }
            stdout(errMsg.red.bold);
        }
    },
    /**
     * Called on report finalizing.
     *
     * @method
     * @instance
     */
    done: () => new Promise(resolve => report.end(resolve)),
};