All files / wdio-utils/src initialiseServices.js

100% Statements 25/25
100% Branches 15/15
100% Functions 1/1
100% Lines 25/25

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          1x                   8x   8x 1x     7x 7x         7x 2x 2x           7x 1x 1x 1x     6x       6x 3x 3x 3x     3x 3x         2x 1x     1x   1x       7x    
import merge from 'deepmerge'
import logger from '@wdio/logger'
 
import initialisePlugin from './initialisePlugin'
 
const log = logger('@wdio/utils:initialiseServices')
 
/**
 * initialise services based on configuration
 * @param  {Object}    config  config of running session
 * @param  {Object}    caps    capabilities of running session
 * @param  {String}    type    define sub type of plugins (for services it could be "launcher")
 * @return {Object[]}          list of service classes that got initialised
 */
export default function initialiseServices (config, caps, type) {
    const initialisedServices = []
 
    if (!Array.isArray(config.services)) {
        return initialisedServices
    }
 
    for (let serviceName of config.services) {
        let serviceConfig = config
 
        /**
         * allow custom services with custom options
         */
        if (Array.isArray(serviceName)) {
            serviceConfig = merge(config, serviceName[1] || {})
            serviceName = serviceName[0]
        }
 
        /**
         * allow custom services that are already initialised
         */
        if (serviceName && typeof serviceName === 'object' && !Array.isArray(serviceName)) {
            log.debug('initialise custom initiated service')
            initialisedServices.push(serviceName)
            continue
        }
 
        try {
            /**
             * allow custom service classes
             */
            if (typeof serviceName === 'function') {
                log.debug(`initialise custom service "${serviceName.name}"`)
                initialisedServices.push(new serviceName(serviceConfig, caps))
                continue
            }
 
            log.debug(`initialise wdio service "${serviceName}"`)
            const Service = initialisePlugin(serviceName, 'service', type)
 
            /**
             * service only contains a launcher
             */
            if (!Service) {
                continue
            }
 
            initialisedServices.push(new Service(serviceConfig, caps))
        } catch(e) {
            log.error(e)
        }
    }
 
    return initialisedServices
}