All files / wdio-cucumber-framework/src reporter.js

100% Statements 59/59
100% Branches 28/28
100% Functions 10/10
100% Lines 57/57

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            22x     22x 22x 22x 22x 22x 22x 22x   22x                   20x   20x                       16x 16x   16x                     1x   1x                           11x 11x     5x 5x   2x 2x       4x   11x 11x           11x 1x     10x 2x       1x 1x         1x   1x           8x         3x 3x 2x         1x         3x 5x 2x 2x 2x           10x 10x                         10x       1x                       1x                     49x   49x 49x 49x   49x       38x 38x 38x 6x          
import { Status } from 'cucumber'
 
import CucumberEventListener from './cucumberEventListener'
import { createStepArgument, getTestParent, getTestStepTitle, getUniqueIdentifier, formatMessage } from './utils'
 
class CucumberReporter {
    gherkinDocEvents = []
 
    constructor (eventBroadcaster, options, cid, specs, reporter) {
        this.capabilities = options.capabilities
        this.tagsInTitle = options.tagsInTitle || false
        this.options = options
        this.cid = cid
        this.specs = specs
        this.reporter = reporter
        this.failedCount = 0
 
        this.eventListener = new CucumberEventListener(eventBroadcaster)
            .on('before-feature', this.handleBeforeFeature.bind(this))
            .on('before-scenario', this.handleBeforeScenario.bind(this))
            .on('before-step', this.handleBeforeStep.bind(this))
            .on('after-step', this.handleAfterStep.bind(this))
            .on('after-scenario', this.handleAfterScenario.bind(this))
            .on('after-feature', this.handleAfterFeature.bind(this))
    }
 
    handleBeforeFeature (uri, feature) {
        this.featureStart = new Date()
 
        this.emit('suite:start', {
            uid: getUniqueIdentifier(feature),
            title: this.getTitle(feature),
            type: 'suite',
            file: uri,
            tags: feature.tags,
            description: feature.description,
            keyword: feature.keyword
        })
    }
 
    handleBeforeScenario (uri, feature, scenario) {
        this.scenarioStart = new Date()
        this.testStart = new Date()
 
        this.emit('suite:start', {
            uid: getUniqueIdentifier(scenario),
            title: this.getTitle(scenario),
            parent: getUniqueIdentifier(feature),
            type: 'suite',
            file: uri,
            tags: scenario.tags
        })
    }
 
    handleBeforeStep (uri, feature, scenario, step, /*sourceLocation*/) {
        this.testStart = new Date()
 
        this.emit('test:start', {
            uid: getUniqueIdentifier(step),
            title: getTestStepTitle(step),
            type: 'test',
            file: uri,
            parent: getTestParent(feature, scenario),
            tags: scenario.tags,
            featureName: feature.name,
            scenarioName: scenario.name,
            argument: createStepArgument(step)
        })
    }
 
    handleAfterStep (uri, feature, scenario, step, result, /*sourceLocation*/) {
        let e = 'undefined'
        switch (result.status) {
        case Status.FAILED:
        case Status.UNDEFINED:
            e = 'fail'
            break
        case Status.PASSED:
            e = 'pass'
            break
        case Status.PENDING:
        case Status.SKIPPED:
        case Status.AMBIGUOUS:
            e = 'pending'
        }
        let error = {}
        let stepTitle = getTestStepTitle(step)
 
        /**
         * if step name is undefined we are dealing with a hook
         * don't report hooks if no error happened
         */
        if (!step.text && result.status !== Status.FAILED) {
            return
        }
 
        if (result.status === Status.UNDEFINED) {
            if (this.options.ignoreUndefinedDefinitions) {
                /**
                 * mark test as pending
                 */
                e = 'pending'
                stepTitle += ' (undefined step)'
            } else {
                /**
                 * mark test as failed
                 */
                this.failedCount++
 
                error = {
                    message: `Step "${stepTitle}" is not defined. You can ignore this error by setting
                              cucumberOpts.ignoreUndefinedDefinitions as true.`,
                    stack: `${step.uri}:${step.line}`
                }
            }
        } else if (result.status === Status.FAILED) {
            /**
             * cucumber failure exception can't get send to parent process
             * for some reasons
             */
            let err = result.exception
            if (err instanceof Error) {
                error = {
                    message: err.message,
                    stack: err.stack
                }
            } else {
                error = {
                    message: err,
                    stack: ''
                }
            }
            this.failedCount++
        } else if (result.status === Status.AMBIGUOUS && this.options.failAmbiguousDefinitions) {
            e = 'fail'
            this.failedCount++
            error = {
                message: result.exception,
                stack: ''
            }
        }
 
        const parent = getTestParent(feature, scenario)
        const payload = {
            uid: getUniqueIdentifier(step),
            title: stepTitle,
            type: 'test',
            file: uri,
            parent: parent,
            error: error,
            duration: new Date() - this.testStart,
            tags: scenario.tags,
            keyword: step.keyword,
            argument: createStepArgument(step)
        }
 
        this.emit('test:' + e, payload)
    }
 
    handleAfterScenario (uri, feature, scenario, sourceLocation) {
        this.emit('suite:end', {
            uid: getUniqueIdentifier(scenario, sourceLocation),
            title: this.getTitle(scenario),
            parent: getUniqueIdentifier(feature),
            type: 'suite',
            file: uri,
            duration: new Date() - this.scenarioStart,
            tags: scenario.tags
        })
    }
 
    handleAfterFeature (uri, feature) {
        this.emit('suite:end', {
            uid: getUniqueIdentifier(feature),
            title: this.getTitle(feature),
            type: 'suite',
            file: uri,
            duration: new Date() - this.featureStart,
            tags: feature.tags
        })
    }
 
    emit (event, payload) {
        let message = formatMessage({ type: event, payload })
 
        message.cid = this.cid
        message.specs = this.specs
        message.uid = payload.uid
 
        this.reporter.emit(message.type, message)
    }
 
    getTitle (featureOrScenario) {
        const name = featureOrScenario.name
        const tags = featureOrScenario.tags
        if (!this.tagsInTitle || !tags.length) return name
        return `${tags.map(tag => tag.name).join(', ')}: ${name}`
    }
}
 
export default CucumberReporter