All files / wdio-runner/src reporter.js

100% Statements 61/61
100% Branches 32/32
100% Functions 13/13
100% Lines 60/60

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          3x   3x 3x 3x                 18x 18x 18x         18x 18x     18x                     10x         10x   10x         24x 24x   24x 42x           24x 18x   18x 18x 18x   18x 4x 1x     3x       23x 3x     20x             18x                           5x 5x 5x 15x 14x 11x   15x 1x 1x           14x 4x 4x     10x                     20x               20x 8x 8x                                   20x 5x 5x 5x 5x 5x                                 15x 14x 14x 14x 13x 13x           1x      
import path from 'path'
import logger from '@wdio/logger'
import { initialisePlugin } from '@wdio/utils'
import { sendFailureMessage } from './utils'
 
const log = logger('@wdio/runner')
 
const NOOP = () => {}
const DEFAULT_SYNC_TIMEOUT = 5000 // 5s
const DEFAULT_SYNC_INTERVAL = 100 // 100ms
 
/**
 * BaseReporter
 * responsible for initialising reporters for every testrun and propagating events
 * to all these reporters
 */
export default class BaseReporter {
    constructor (config, cid, caps) {
        this.config = config
        this.cid = cid
        this.caps = caps
 
        /**
         * these configurations are not publicly documented as there should be no desire for it
         */
        this.reporterSyncInterval = this.config.reporterSyncInterval || DEFAULT_SYNC_INTERVAL
        this.reporterSyncTimeout = this.config.reporterSyncTimeout || DEFAULT_SYNC_TIMEOUT
 
        // ensure all properties are set before initializing the reporters
        this.reporters = config.reporters.map(::this.initReporter)
 
    }
 
    /**
     * emit events to all registered reporter and wdio launcer
     *
     * @param  {String} e       event name
     * @param  {object} payload event payload
     */
    emit (e, payload) {
        payload.cid = this.cid
 
        /**
         * Send failure message (only once) in case of test or hook failure
         */
        sendFailureMessage(e, payload)
 
        this.reporters.forEach((reporter) => reporter.emit(e, payload))
    }
 
    getLogFile(name) {
        // clone the config to avoid changing original properties
        let options = Object.assign({}, this.config)
        let filename = `wdio-${this.cid}-${name}-reporter.log`
 
        const reporterOptions = this.config.reporters.find((reporter) => (
            Array.isArray(reporter) && (
                reporter[0] === name ||
                typeof reporter[0] === 'function' && reporter[0].name === name
            )
        ))
 
        if(reporterOptions) {
            const fileformat = reporterOptions[1].outputFileFormat
 
            options.cid = this.cid
            options.capabilities = this.caps
            Object.assign(options, reporterOptions[1])
 
            if (fileformat) {
                if (typeof fileformat !== 'function') {
                    throw new Error('outputFileFormat must be a function')
                }
 
                filename = fileformat(options)
            }
        }
 
        if (!options.outputDir) {
            return
        }
 
        return path.join(options.outputDir, filename)
    }
 
    /**
     * return write stream object based on reporter name
     */
    getWriteStreamObject (reporter) {
        return {
            write: /* istanbul ignore next */ (content) => process.send({
                origin: 'reporter',
                name: reporter,
                content
            })
        }
    }
 
    /**
     * wait for reporter to finish synchronization, e.g. when sending data asynchronous
     * to a server (e.g. sumo reporter)
     */
    waitForSync () {
        const startTime = Date.now()
        return new Promise((resolve, reject) => {
            const interval = setInterval(() => {
                const unsyncedReporter = this.reporters
                    .filter((reporter) => !reporter.isSynchronised)
                    .map((reporter) => reporter.constructor.name)
 
                if ((Date.now() - startTime) > this.reporterSyncTimeout && unsyncedReporter.length) {
                    clearInterval(interval)
                    return reject(new Error(`Some reporters are still unsynced: ${unsyncedReporter.join(', ')}`))
                }
 
                /**
                 * no reporter are in need to sync anymore, continue
                 */
                if (!unsyncedReporter.length) {
                    clearInterval(interval)
                    return resolve(true)
                }
 
                log.info(`Wait for ${unsyncedReporter.length} reporter to synchronise`)
                // wait otherwise
            }, this.reporterSyncInterval)
        })
    }
 
    /**
     * initialise reporters
     */
    initReporter (reporter) {
        let ReporterClass
        let options = {
            logLevel: this.config.logLevel,
            setLogFile: NOOP
        }
 
        /**
         * check if reporter has custom options
         */
        if (Array.isArray(reporter)) {
            options = Object.assign({}, options, reporter[1])
            reporter = reporter[0]
        }
 
        /**
         * check if reporter was passed in from a file, e.g.
         *
         * ```js
         * const MyCustomeReporter = require('/some/path/MyCustomeReporter.js')
         * export.config = {
         *     //...
         *     reporters: [
         *         MyCustomeReporter, // or
         *         [MyCustomeReporter, { custom: 'option' }]
         *     ]
         *     //...
         * }
         * ```
         */
        if (typeof reporter === 'function') {
            ReporterClass = reporter
            const customLogFile = options.setLogFile(this.cid, ReporterClass.name)
            options.logFile = customLogFile || this.getLogFile(ReporterClass.name)
            options.writeStream = this.getWriteStreamObject(ReporterClass.name)
            return new ReporterClass(options)
        }
 
        /**
         * check if reporter is a node package, e.g. wdio-dot reporter
         *
         * ```js
         * export.config = {
         *     //...
         *     reporters: [
         *         'dot', // or
         *         ['dot', { custom: 'option' }]
         *     ]
         *     //...
         * }
         * ```
         */
        if (typeof reporter === 'string') {
            ReporterClass = initialisePlugin(reporter, 'reporter')
            const customLogFile = options.setLogFile(this.cid, reporter)
            options.logFile = customLogFile || this.getLogFile(reporter)
            options.writeStream = this.getWriteStreamObject(reporter)
            return new ReporterClass(options)
        }
 
        /**
         * throw error if reporter property was invalid
         */
        throw new Error('Invalid reporters config')
    }
}