All files / lib utils.js

100% Statements 62/62
100% Branches 32/32
100% Functions 10/10
100% Lines 58/58

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                2x 2x   2x 2x 2x   2x             2x 415x 415x 415x               2x 3x   3x 1x   3x 2x   3x 2x   3x 2x   3x 2x   3x   3x 2x   1x                     2x 5x 5x   3x   7x 7x 2x     3x 2x       2x 4x 2x 2x 2x 2x 2x 2x         2x 4x 2x 2x 2x     2x 7x 7x   7x 1x         6x 1x     5x   7x 7x      
"use strict";
 
/**
 * Utils.
 *
 * @module
 */
 
const path = require("path");
const util = require("util");
 
const _ = require("lodash");
const U = require("glace-utils");
const LOG = U.logger;
 
const CONF = require("./config");
 
/**
* Helper to set actual log file.
*
* @function
*/
module.exports.setLog = () => {
    const logsDir = CONF.report.testDir || CONF.report.dir;
    const logFile = path.resolve(logsDir, "logs", "test.log");
    LOG.setFile(logFile);
};
 
/**
 * Account test error and add it to test or session errors.
 *
 * @function
 */
module.exports.accountError = (errMsg, err) => {
    errMsg = errMsg || "";
 
    if (CONF.test.curCase && !_.isEmpty(CONF.test.curCase.testParams)) {
        errMsg += "\n" + util.format(CONF.test.curCase.testParams);
    }
    if (err.message) {
        errMsg += "\nmessage: " + err.message;
    }
    if (err.diff) {
        errMsg += err.diff;
    }
    if (err.stack) {
        errMsg += "\nstack: " + err.stack;
    }
    if (err.seleniumStack) {
        errMsg += "\nselenium: " + JSON.stringify(err.seleniumStack, null, "\t");
    }
    errMsg = errMsg.trim();
 
    if (CONF.test.curCase) {
        CONF.test.curCase.addError(errMsg);
    } else {
        CONF.session.errors.push(errMsg);
    }
};
 
/**
 * Get function description.
 *
 * @function
 * @arg {function} func - Function to read documentation.
 * @return {string} Documentation.
 */
module.exports.getDoc = func => {
    let doc = func.__doc__;
    if (!doc) return "";
 
    doc = doc
        .split("\n")
        .map(i => i.trim())
        .filter(i => i)
        .map(i => `   ${i}`)
        .join("\n");
 
    if (!doc) return "";
    return `  /**\n${doc}\n   */`;
};
 
 
module.exports.printTestErrors = (failedTests, log = console.log) => {
    if (!failedTests.length) return;
    log();
    log("TEST FAILURES:".bold);
    for (const testCase of failedTests) {
        log();
        log(("test: " + testCase.name).cyan.bold);
        printErrors(testCase.errors, log);
    }
};
 
 
module.exports.printSessionErrors = (log = console.log) => {
    if (!CONF.session.errors.length) return;
    log();
    log("OUTTEST FAILURES:".bold);
    printErrors(CONF.session.errors, log);
};
 
const printErrors = (errors, log) => {
    for (let error of errors) {
        error = error.split("\n").map(line => {
 
            if (line.trim() === "+ expected - actual") {
                return line
                    .replace("+ expected", "+ expected".green.bold)
                    .replace("- actual", "- actual".red.bold);
            }
 
            if (line.trim().startsWith("+")) {
                return line.green.bold;
            }
 
            return line.red.bold;
        }).join("\n");
        log();
        log(error);
    }
};