All files / lib/steps timer.js

100% Statements 28/28
100% Branches 2/2
100% Functions 5/5
100% Lines 28/28

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                            1x   1x 1x   1x   1x                                       2x 2x 1x 1x 1x                                   5x 5x 5x                             21x 21x 21x                                     4x 4x 4x 4x 4x                                           5x 1x   4x     5x 5x 3x       1x  
/* global expect */
 
"use strict";
 
/**
 * Steps to measure time.
 * 
 * These methods will be mixed with glacejs [Steps](https://glacejs.github.io/glace-core/Steps.html)
 * class and available via its instance [$](https://glacejs.github.io/glace-core/global.html#$)
 * in tests.
 *
 * @mixin TimerSteps
 */
 
var util = require("util");
 
var U = require("glace-utils");
var LOG = U.logger;
 
var A = require("../allure");
 
var TimerSteps = {
 
    pause: async function (timeout, message) {
        /**
         * Step to make pause in another step or test case. **Good style** is
         * to not use directly in test case, only inside another step.
         *
         * @async
         * @memberOf TimerSteps
         * @method pause
         * @instance
         * @arg {number} timeout - Pause time, sec.
         * @arg {string} message - Pause reason.
         * @return {Promise<void>}
         * @throws {AssertionError} If pause message is not defined.
         * @example
         *
         * await $.pause(1, "wait for server start");
         */
 
        A.step(`Sleep ${timeout} sec because ${message}`);
        expect(message, "Pause message is not defined").to.not.be.undefined;
        LOG.warn(util.format("Sleep", timeout, "sec, reason:", message));
        await U.sleep(timeout * 1000);
        A.pass();
    },
 
    startTimer: function () {
        /**
         * Step to start timer.
         * Each time when it will be called, timer will be reset.
         *
         * @memberOf TimerSteps
         * @method startTimer
         * @instance
         * @example
         *
         * $.startTimer();
         * await $.pause(1, "sleep a bit");
         * var elapsedSeconds = $.getTimer();
         */
 
        A.step("Start timer");
        this._timer = new Date();
        A.pass();
    },
 
    stopTimer: function () {
        /**
         * Step to stop timer.
         *
         * @memberOf TimerSteps
         * @method stopTimer
         * @instance
         * @example
         *
         * $.stopTimer();
         */
 
        A.step("Stop timer");
        this._timer = null;
        A.pass();
    },
 
    getTimer: function () {
        /**
         * Step to get timer.
         *
         * @memberOf TimerSteps
         * @method getTimer
         * @instance
         * @return {number} Number of seconds since timer starts.
         * @throws {AssertionError} If timer is not started.
         * @example
         *
         * $.startTimer();
         * await $.pause(1, "sleep a bit");
         * var elapsedSeconds = $.getTimer();
         */
 
        A.step("Get timer value");
        expect(this._timer, "Timer is not started").to.exist;
        var result = (new Date() - this._timer) / 1000;
        A.pass();
        return result;
    },
 
    checkTimer: function (condition) {
        /**
         * Step to check timer.
         *
         * @memberOf TimerSteps
         * @method checkTimer
         * @instance
         * @arg {string|object} condition - [chaijs](http://chaijs.com/) condition.
         * @throws {AssertionError} If timer verification was failed.
         * @example
         *
         * $.startTimer();
         * $.checkTimer("to exist");
         *
         * $.startTimer();
         * await $.pause(1, "sleep a bit");
         * $.checkTimer({ "to be gte": 1 });
         */
 
        if (typeof(condition) === "string") {
            var stepMsg = `Check timer ${condition}`;
        } else {
            stepMsg = util.format("Check timer with condition", condition);
        }
 
        A.step(stepMsg);
        expect(this.getTimer(), "Timing is failed").to.correspond(condition);
        A.pass();
    },
};
 
module.exports = TimerSteps;