All files / lib loader.js

100% Statements 50/50
100% Branches 24/24
100% Functions 6/6
100% Lines 49/49

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                                    2x 2x   2x 2x 2x   2x 2x 2x                       2x 6x   6x 1x     6x 5x       3x                     2x 6x   4x 1x       3x 3x 1x                   2x   4x 1x 1x 1x         4x 4x 2x 2x   2x                     2x 4x   31x 31x   31x 1x     31x 30x   30x 29x           2x 32x 32x 32x     2x 2x 2x  
/* global before session */
 
"use strict";
 
/**
 * Makes tests root session.
 *
 * - runner loads root `conftest.js` if it is located on one level with
 *   each of `CONF.test.dirs`;
 * - if each of `CONF.test.dirs` is file with tests, runner loads and executes it;
 * - if each of `CONF.test.dirs` is folder runner loads files inside recursive if
 *   file name starts with `test` and ends with `.js`;
 * - inside each subfolder of each of `CONF.test.dirs` runner loads `conftest.js`
 *   file if it is present;
 *
 * @module
 */
 
const fs = require("fs");
const path = require("path");
 
const _ = require("lodash");
const expect = require("chai").expect;
const U = require("glace-utils");
 
require("./globals");
const CONF = require("./config");
const ConfigError = require("./error").ConfigError;
 
/**
 * Loads special `preloads` files before main conftests and test files.
 *
 * Preloads are specified in `CONF.preloads` array. It may be managed only
 * programmatically and needs as extension point to load some custom files
 * before tests.
 *
 * After preloads it loads root (the mainest) conftest file, which may be set
 * via CLI.
 */
const preloads = () => {
    const pre = _.clone(CONF.session.preloads);
 
    if (CONF.session.rootConftest && !pre.includes(CONF.session.rootConftest)) {
        pre.push(CONF.session.rootConftest);
    }
 
    for (const preload of pre) {
        expect(
            fs.existsSync(preload) && fs.statSync(preload).isFile(),
            `Preloader '${preload}' isn't a file or doesn't exist`
        ).to.be.true;
        require(preload);
    }
};
 
/**
 * Main conftests are loaded before tests session creation and may used for
 * objects management, for example to created custom instance of global `SS`.
 *
 * Main conftest is `conftest.js` file which is located on one hierarchy level
 * with each specified tests folder or file.
 */
const mainConftests = () => {
    for (const testDir of CONF.test.dirs) {
 
        if (!fs.existsSync(testDir)) {
            throw new ConfigError(
                `Tests file or folder '${testDir}' doesn't exist`);
        }
 
        const siblingConftest = path.resolve(path.dirname(testDir), "conftest.js");
        if (fs.existsSync(siblingConftest)) {
            require(siblingConftest);
        }
    }
};
 
/**
 * Callback to create tests session.
 *
 * It kills some processes before all if they are specified.
 */
const sessFunc = () => {
 
    if (CONF.session.killProcs) {
        before(async () => {
            for (const procName of CONF.session.killProcs) {
                await U.killProcs(procName);
            }
        });
    }
 
    for (const testDir of CONF.test.dirs) {
        if (!fs.statSync(testDir).isDirectory()) {
            reload(testDir);
            continue;
        }
        loadTests(testDir);
    }
};
 
/**
 * Loads test files recursively. Test file name should start with `test` and
 * end with `.js`.
 *
 * @function
 * @arg {string} dir - Folder with test files.
 */
const loadTests = dir => {
    for (const fileName of fs.readdirSync(dir)) {
 
        const filePath = path.resolve(dir, fileName);
        const fileStat = fs.statSync(filePath);
 
        if (fileStat.isDirectory()) {
            loadTests(filePath);
        }
 
        if (fileStat.isFile()) {
            if (fileName === "conftest.js") require(filePath);
 
            if (fileName.startsWith("test") && fileName.endsWith(".js")) {
                reload(filePath);
            }
        }
    }
};
 
const reload = filePath => {
    const fullPath = path.resolve(filePath);
    delete require.cache[fullPath];
    return require(fullPath);
};
 
preloads();
mainConftests();
session(sessFunc);