All files / lib/reporter base.js

100% Statements 134/134
100% Branches 60/60
100% Functions 23/23
100% Lines 113/113

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                  2x 2x   2x 2x 2x 2x 2x   2x 2x 2x             2x   2x 60x   60x 4x 4x       60x 209x   7x 6x     7x   7x 2x     7x 4x       60x 242x   241x           241x 488x       60x 242x   241x           241x 488x     241x 207x 207x 207x       60x 534x 1064x       60x 534x 1064x       60x 1691x 3378x       60x 1691x 3378x       60x   534x 534x   534x 534x 1065x       60x   4x 2x 2x   2x 1x           4x   4x 3x     4x 1x 1x       60x 3x 2x         2x 2x                 2x 4x 4x 5x 3x 3x 1x     4x                 2x 5x 8x 7x                     2x 2x 2x 2x             2x 535x 534x   533x 533x 533x             2x 534x 533x   1x 1x     2x   5x 2x 2x   1x 1x       4x 4x 1x     4x 4x     1x      
"use strict";
/**
 * `GlaceJS` common reporter.
 *
 * @class
 * @name GlaceReporter
 * @arg {object} runner - `MochaJS` runner.
 */
 
const fs = require("fs");
const util = require("util");
 
const _ = require("lodash");
const colors = require("colors");
const MochaReporter = require("mocha").reporters.base;
const U = require("glace-utils");
const LOG = U.logger;
 
const CONF = require("../config");
const TestCase = require("../testing").TestCase;
const utils = require("../utils");
 
/**
 * Registered reporters.
 *
 * @type {object[]}
 */
let reporters = [];
 
const GlaceReporter = function (runner) {
    MochaReporter.call(this, runner);
 
    runner.on("start", () => {
        for (const reporter of reporters) {
            if (reporter.start) reporter.start();
        }
    });
 
    runner.on("end", () => {
        const failedTests = CONF.test.cases.filter(t => t.status === TestCase.FAILED);
 
        if (!failedTests.length) {
            if (!CONF.session.errors.length) CONF.session.isPassed = true;
        }
 
        saveFailedTests(failedTests);
 
        if (fs.existsSync(CONF.report.dir)) {
            U.clearEmptyFolders(CONF.report.dir);
        }
 
        for (const reporter of reporters) {
            if (reporter.end) reporter.end();
        }
    });
 
    runner.on("suite", mochaSuite => {
        if (mochaSuite.root) return; // skip mocha root suite;
 
        const methodName = {
            undefined: "scope",
            suite: "suite",
            test: "test",
        }[mochaSuite.title.type];
 
        for (const reporter of reporters) {
            if (reporter[methodName]) reporter[methodName](mochaSuite);
        }
    });
 
    runner.on("suite end", mochaSuite => {
        if (mochaSuite.root) return; // skip mocha root suite;
 
        const methodName = {
            undefined: "scopeEnd",
            suite: "suiteEnd",
            test: "testEnd",
        }[mochaSuite.title.type];
 
        for (const reporter of reporters) {
            if (reporter[methodName]) reporter[methodName](mochaSuite);
        }
 
        if (methodName === "testEnd") {
            CONF.test.curCase = null;
            CONF.report.testDir = null;
            utils.setLog(); // Current test case is finished, need to reinit log
        }
    });
 
    runner.on("test", mochaTest => {
        for (const reporter of reporters) {
            if (reporter.chunk) reporter.chunk(mochaTest);
        }
    });
 
    runner.on("test end", mochaTest => {
        for (const reporter of reporters) {
            if (reporter.chunkEnd) reporter.chunkEnd(mochaTest);
        }
    });
 
    runner.on("hook", mochaHook => {
        for (const reporter of reporters) {
            if (reporter.hook) reporter.hook(mochaHook);
        }
    });
 
    runner.on("hook end", mochaHook => {
        for (const reporter of reporters) {
            if (reporter.hookEnd) reporter.hookEnd(mochaHook);
        }
    });
 
    runner.on("pass", mochaTest => {
 
        passChunkId();
        handleSkipState(mochaTest);
    
        const method = mochaTest.state === "skipped" ? "skip" : "pass";
        for (const reporter of reporters) {
            if (reporter[method]) reporter[method](mochaTest);
        }
    });
 
    runner.on("fail", (mochaTest, err) => {
 
        if (err.actual && err.expected) {
            try {
                err.diff = colors.strip(
                    MochaReporter.generateDiff(err.actual, err.expected));
                if (err.diff.trim() === "+ expected - actual") {
                    delete err.diff;
                }
            } catch (e) {
                /* do nothing */
            }
        }
        utils.accountError(mochaTest.title, err);
 
        for (const reporter of reporters) {
            if (reporter.fail) reporter.fail(mochaTest, err);
        }
 
        if (CONF.session.exitOnFail) {
            CONF.test.curCase.end(TestCase.FAILED);
            runner.emit("end");
        }
    });
 
    runner.on("pending", mochaTest => {
        for (const reporter of reporters) {
            if (reporter.pending) reporter.pending(mochaTest);
        }
    });
};
 
util.inherits(GlaceReporter, MochaReporter);
module.exports = GlaceReporter;
/**
 * Finalizes reporting.
 *
 * @function
 * @async
 * @arg {Array.<*>} failures - Tests failures.
 * @arg {function} fn - Finalizator.
 */
GlaceReporter.prototype.done = function (failures, fn) {
    let prms = Promise.resolve();
    reporters.forEach(reporter => {
        if (reporter.done) {
            prms = prms
                .then(() => reporter.done())
                .catch(e => LOG.error(e));
        }
    });
    return prms.then(() => fn(failures));
};
/**
 * Registers reporters if they are not.
 *
 * @method
 * @static
 * @arg {...object} reporters - Sequence of reporters to register.
 */
GlaceReporter.register = function () {
    for (const reporter of arguments) {
        if (!reporters.includes(reporter)) {
            reporters.push(reporter);
        }
    }
};
/**
 * Removes reporters if they are registered.
 *
 * @method
 * @static
 * @arg {...object} reporters - Sequence of reporters to remove.
 */
GlaceReporter.remove = function () {
    const args = Array.from(arguments);
    args.unshift(reporters);
    reporters = _.without.apply(_, args);
};
 
/**
 * Mark chunk as passed via its ID.
 * @ignore
 */
const passChunkId = () => {
    if (!CONF.chunk.curId) return;
    if (CONF.chunk.passedIds.includes(CONF.chunk.curId)) return;
 
    CONF.chunk.passedIds.push(CONF.chunk.curId);
    if (CONF.test.curCase) CONF.test.curCase.addPassedChunkId(CONF.chunk.curId);
    CONF.chunk.curId = null;
};
 
/**
 * Handle skip state of mocha test.
 * @ignore 
 */
const handleSkipState = mochaTest => {
    if (!CONF.test.curCase) return;
    if (CONF.test.curCase.skipChunk !== mochaTest.title) return;
 
    mochaTest.state = "skipped";
    CONF.test.curCase.skipChunk = null;
};
 
const saveFailedTests = failedTests => {
 
    if (fs.existsSync(CONF.report.failedTestsPath)) {
        try {
            fs.unlinkSync(CONF.report.failedTestsPath);
        } catch (e) {
            LOG.error(util.format(`Can't remove file '${CONF.report.failedTestsPath}'`, e));
            return;
        }
    }
 
    const data = [];
    for (const failedTest of failedTests) {
        data.push({ id: failedTest.id, passed_chunk_ids: failedTest.passedChunkIds });
    }
 
    try {
        fs.writeFileSync(
            CONF.report.failedTestsPath, JSON.stringify(data, null, "  "));
    } catch (e) {
        LOG.error(util.format(`Can't write file '${CONF.report.failedTestsPath}'`, e));
    }
};