All files / wdio-reporter/src index.js

100% Statements 43/43
93.75% Branches 15/16
100% Functions 4/4
100% Lines 43/43

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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226                                  38x 38x     38x 2x     37x     37x 37x 37x 37x 37x 37x                     37x       37x   37x 37x   37x           37x                 37x               37x             37x               37x               37x                                     37x 6x 6x             6x 2x   6x 6x 5x   1x     6x 6x 6x 6x 6x     37x         37x             37x                     37x           37x                         1x             3x                                                                
import fs from 'fs'
import fse from 'fs-extra'
import { format } from 'util'
import EventEmitter from 'events'
 
import { getErrorsFromEvent } from './utils'
 
import SuiteStats from './stats/suite'
import HookStats from './stats/hook'
import TestStats from './stats/test'
 
import RunnerStats from './stats/runner'
 
import { MOCHA_TIMEOUT_MESSAGE, MOCHA_TIMEOUT_MESSAGE_REPLACEMENT } from './constants'
 
export default class WDIOReporter extends EventEmitter {
    constructor (options) {
        super()
        this.options = options
 
        // ensure the report directory exists
        if (this.options.outputDir) {
            fse.ensureDirSync(this.options.outputDir)
        }
 
        this.outputStream = this.options.stdout || !this.options.logFile
            ? options.writeStream
            : fs.createWriteStream(this.options.logFile)
        this.failures = []
        this.suites = {}
        this.hooks = {}
        this.tests = {}
        this.currentSuites = []
        this.counts = {
            suites: 0,
            tests: 0,
            hooks: 0,
            passes: 0,
            skipping: 0,
            failures: 0
        }
 
        let currentTest
 
        const rootSuite = new SuiteStats({
            title: '(root)',
            fullTitle: '(root)',
        })
        this.currentSuites.push(rootSuite)
 
        this.on('client:beforeCommand', ::this.onBeforeCommand)
        this.on('client:afterCommand', ::this.onAfterCommand)
 
        this.on('runner:start',  /* istanbul ignore next */ (runner) => {
            rootSuite.cid = runner.cid
            this.runnerStat = new RunnerStats(runner)
            this.onRunnerStart(this.runnerStat)
        })
 
        this.on('suite:start',  /* istanbul ignore next */ (params) => {
            const suite = new SuiteStats(params)
            const currentSuite = this.currentSuites[this.currentSuites.length - 1]
            currentSuite.suites.push(suite)
            this.currentSuites.push(suite)
            this.suites[suite.uid] = suite
            this.onSuiteStart(suite)
        })
 
        this.on('hook:start',  /* istanbul ignore next */ (hook) => {
            const hookStat = new HookStats(hook)
            const currentSuite = this.currentSuites[this.currentSuites.length - 1]
            currentSuite.hooks.push(hookStat)
            this.hooks[hook.uid] = hookStat
            this.onHookStart(hookStat)
        })
 
        this.on('hook:end',  /* istanbul ignore next */ (hook) => {
            const hookStat = this.hooks[hook.uid]
            hookStat.complete(getErrorsFromEvent(hook))
            this.counts.hooks++
            this.onHookEnd(hookStat)
        })
 
        this.on('test:start',  /* istanbul ignore next */ (test) => {
            currentTest = new TestStats(test)
            const currentSuite = this.currentSuites[this.currentSuites.length - 1]
            currentSuite.tests.push(currentTest)
            this.tests[test.uid] = currentTest
            this.onTestStart(currentTest)
        })
 
        this.on('test:pass',  /* istanbul ignore next */ (test) => {
            const testStat = this.tests[test.uid]
            testStat.pass()
            this.counts.passes++
            this.counts.tests++
            this.onTestPass(testStat)
        })
 
        this.on('test:fail',  /* istanbul ignore next */ (test) => {
            const testStat = this.tests[test.uid]
 
            /**
             * replace "Ensure the done() callback is being called in this test." with more meaningful
             * message (Mocha only)
             */
            if (test.error && test.error.message && test.error.message.includes(MOCHA_TIMEOUT_MESSAGE)) {
                let replacement = format(MOCHA_TIMEOUT_MESSAGE_REPLACEMENT, test.parent, test.title)
                test.error.message = test.error.message.replace(MOCHA_TIMEOUT_MESSAGE, replacement)
                test.error.stack = test.error.stack.replace(MOCHA_TIMEOUT_MESSAGE, replacement)
            }
 
            testStat.fail(getErrorsFromEvent(test))
            this.counts.failures++
            this.counts.tests++
            this.onTestFail(testStat)
        })
 
        this.on('test:pending', (test) => {
            const currentSuite = this.currentSuites[this.currentSuites.length - 1]
            currentTest = new TestStats(test)
 
            /**
             * In Mocha: tests that are skipped don't have a start event but a test end.
             * In Jasmine: tests have a start event, therefor we need to replace the
             * test instance with the pending test here
             */
            if (test.uid in this.tests && this.tests[test.uid].state !== 'pending') {
                currentTest.uid = test.uid in this.tests ? 'skipped-' + this.counts.skipping : currentTest.uid
            }
            const suiteTests = currentSuite.tests
            if (!suiteTests.length || currentTest.uid !== suiteTests[suiteTests.length - 1].uid) {
                currentSuite.tests.push(currentTest)
            } else {
                suiteTests[suiteTests.length - 1] = currentTest
            }
 
            this.tests[currentTest.uid] = currentTest
            currentTest.skip()
            this.counts.skipping++
            this.counts.tests++
            this.onTestSkip(currentTest)
        })
 
        this.on('test:end',  /* istanbul ignore next */ (test) => {
            const testStat = this.tests[test.uid]
            this.onTestEnd(testStat)
        })
 
        this.on('suite:end',  /* istanbul ignore next */ (suite) => {
            const suiteStat = this.suites[suite.uid]
            suiteStat.complete()
            this.currentSuites.pop()
            this.onSuiteEnd(suiteStat)
        })
 
        this.on('runner:end',  /* istanbul ignore next */ (runner) => {
            rootSuite.complete()
            this.runnerStat.failures = runner.failures
            this.runnerStat.retries = runner.retries
            this.runnerStat.complete()
            this.onRunnerEnd(this.runnerStat)
        })
 
        /**
         * browser client event handlers
         */
        this.on('client:command',  /* istanbul ignore next */ (payload) => {
            if (!currentTest) {
                return
            }
            currentTest.output.push(Object.assign(payload, { type: 'command' }))
        })
        this.on('client:result',  /* istanbul ignore next */ (payload) => {
            if (!currentTest) {
                return
            }
            currentTest.output.push(Object.assign(payload, { type: 'result' }))
        })
    }
 
    /**
     * allows reporter to stale process shutdown process until required sync work
     * is done (e.g. when having to send data to some server or any other async work)
     */
    get isSynchronised () {
        return true
    }
 
    /**
     * function to write to reporters output stream
     */
    write (content) {
        this.outputStream.write(content)
    }
 
    /* istanbul ignore next */
    onRunnerStart () {}
    /* istanbul ignore next */
    onBeforeCommand () {}
    /* istanbul ignore next */
    onAfterCommand () {}
    /* istanbul ignore next */
    onScreenshot () {}
    /* istanbul ignore next */
    onSuiteStart () {}
    /* istanbul ignore next */
    onHookStart () {}
    /* istanbul ignore next */
    onHookEnd () {}
    /* istanbul ignore next */
    onTestStart () {}
    /* istanbul ignore next */
    onTestPass () {}
    /* istanbul ignore next */
    onTestFail () {}
    /* istanbul ignore next */
    onTestSkip () {}
    /* istanbul ignore next */
    onTestEnd () {}
    /* istanbul ignore next */
    onSuiteEnd () {}
    /* istanbul ignore next */
    onRunnerEnd () {}
}