All files / lib/globals chunk.js

100% Statements 44/44
96.15% Branches 25/26
100% Functions 4/4
100% Lines 37/37

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    2x 2x   2x                                                                                                                                 2x   538x 61x 61x 61x     538x 476x 476x     538x 1x 1x     538x 538x   538x 538x   538x   537x 2x   535x     536x             538x 538x 537x 537x   537x 537x   537x   537x 37x 37x 37x     500x     537x     2x  
"use strict";
 
const _ = require("lodash");
const isPromise = require("is-promise");
 
const CONF = require("../config");
 
/**
 * Global function, existing in `glace` tests, which creates test chunk.
 *
 * `chunk` is independently executed part of `test`. It means that even if
 * first chunk is failed, other will be executed in any case. `test` should
 * contain as minimum one `chunk`.
 *
 * @global
 * @function
 * @arg {string} [name=null] - Name of chunk.
 * @arg {object} [opts] - Chunk options.
 * @arg {?number} [opts.retry=null] - Number of chunk retries on failure. Overrides
 * [config](GlaceConfig.html#test-chunk-retry) and [test](#test-chunk-retry) settings.
 * @arg {?number} [opts.timeout=null] - Time limit to execute chunk, **sec**. Overrides
 * [config](GlaceConfig.html#test-chunk-timeout) and [test](#test-chunk-timeout) settings.
 * @arg {function} func - Callback function with test payload. Can be `async` too.
 *
 * @example <caption><b>Anonymous chunk</b></caption>
 *
 * test("My test", () => {
 *     chunk(() => {
 *         var a = 5;
 *         expect(a).to.be.equal(2);
 *     });
 * });
 *
 * @example <caption><b>Named chunk</b></caption>
 *
 * test("My test", () => {
 *     chunk("My chunk", () => {
 *         var a = 5;
 *         expect(a).to.be.equal(2);
 *     });
 * });
 *
 * @example <caption><b>Chunk with options</b></caption>
 *
 * test("My test", () => {
 *     chunk("My chunk", { retry: 2, timeout: 1 }, () => {
 *         var a = 5;
 *         expect(a).to.be.equal(2);
 *     });
 * });
 *
 * @example <caption><b>Several chunks in test</b></caption>
 *
 * test("My test", () => {
 *     chunk("first chunk", () => {
 *         expect(2).to.be.equal(3);
 *     });
 *     chunk("second chunk", () => {
 *         expect(3).to.be.equal(3);
 *     });
 * });
 *
 * @example <caption><b>Async chunk</b></caption>
 *
 * test("My test", () => {
 *     chunk(async () => {
 *         await Promise.resolve("done!");
 *     });
 * });
 */
const chunk = (name, opts, func) => {
 
    if (name instanceof Function) {
        func = name;
        name = "";
        opts = {};
    }
 
    if (opts instanceof Function) {
        func = opts;
        opts = {};
    }
 
    if (name instanceof Object) {
        opts = name;
        name = "";
    }
 
    name = name || "";
    opts = opts || {};
 
    CONF.chunk.id++;
    const chunkId = CONF.test.id + "_" + CONF.chunk.id;
 
    if (CONF.chunk.passedIds.includes(chunkId)) return;
 
    if (CONF.retry.id) {
        if (!_.flatMap(CONF.retry.chunkIds).includes(chunkId)) return;
    } else {
        CONF.retry.curChunkIds.push(chunkId);
    };
 
    it(name, _chunkCb(name, chunkId, opts, func));
};
 
/**
 * Chunk callback.
 * @ignore
 */
const _chunkCb = (name, chunkId, opts, func) => function () {
    expect(CONF.test.curCase, "Oops! Chunk is used outside of test").to.exist;
    CONF.test.curCase.addChunk(name);
    CONF.chunk.curId = chunkId;
 
    if (opts.retry) this.retries(opts.retry);
    if (opts.timeout) this.timeout(opts.timeout * 1000);
 
    let result = func();
 
    if (isPromise(result)) {
        result = result.then(r => {
            if (r === false) CONF.test.curCase.skipChunk = name;
            return r;
        });
    } else {
        if (result === false) CONF.test.curCase.skipChunk = name;
    }
 
    return result;
};
 
module.exports = chunk;