All files / lib/globals session.js

100% Statements 26/26
100% Branches 12/12
100% Functions 5/5
100% Lines 20/20

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    2x   2x                                                               2x   6x 6x 6x   6x 6x 6x   6x             2x 2x 2x             3x 3x 3x 1x 1x 1x     2x  
"use strict";
 
const _ = require("lodash");
 
const CONF = require("../config");
 
/**
 * Executes tests session.
 *
 * @global
 * @function
 * @arg {string} [name] - Session name. By default it includes start date time.
 * @arg {object} [fixtures=[]] - Session fixtures.
 * @arg {function} func - Function with test cases.
 * @example
 
session(() => {
    test("Test #1", () => {
        chunk("Chunk #1", () => {
            someFunc();
        });
        chunk("Chunk #2", () => {
            someFunc();
        });
    });
    test("Test #2", () => {
        chunk("Chunk #1", () => {
            someFunc();
        });
        chunk("Chunk #2", () => {
            someFunc();
        });
    });
});
 
 */
const session = (name, fixtures, func) => {
 
    if (_.isFunction(fixtures)) [func, fixtures] = [fixtures];
    if (_.isArray(name)) [fixtures, name] = [name];
    if (_.isFunction(name)) [func, name] = [name];
 
    name = name || CONF.session.name;
    fixtures = fixtures || [];
    CONF.session.errors = [];
 
    suite(name, fixtures, sessCb(name, fixtures, func));
};
 
/**
 * Session callback.
 * @ignore
 */
const sessCb = (name, fixtures, func) => () => {
    func();
    after(afterCb(name, fixtures, func));
};
 
/**
 * After-hook callback.
 * @ignore
 */
const afterCb = (name, fixtures, func) => () => {
    delete CONF.retry.chunkIds[CONF.retry.id];
    if (_.isEmpty(CONF.retry.chunkIds)) return;
    CONF.retry.id++;
    CONF.test.id = 0;
    suite(name, fixtures, sessCb(name, fixtures, func));
};
 
module.exports = session;