All files / wdio-cli/src watcher.js

100% Statements 42/42
100% Branches 20/20
100% Functions 13/13
100% Lines 38/38

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                1x       9x 9x 9x   9x 9x 16x   9x 9x             3x             3x 3x 2x               3x         3x 3x       2x 1x     1x                   10x                       9x   9x 3x           9x 12x     9x               3x 6x         3x 1x             2x         2x         2x 3x 3x 3x 3x         1x      
import chokidar from 'chokidar'
import logger from '@wdio/logger'
import pickBy from 'lodash.pickby'
import flattenDeep from 'lodash.flattendeep'
import union from 'lodash.union'
 
import Launcher from './launcher.js'
 
const log = logger('@wdio/cli:watch')
 
export default class Watcher {
    constructor (configFile, argv) {
        log.info('Starting launcher in watch mode')
        this.launcher = new Launcher(configFile, argv, true)
        this.argv = argv
 
        const specs = this.launcher.configParser.getSpecs()
        const capSpecs = this.launcher.isMultiremote ? [] : union(flattenDeep(
            this.launcher.configParser.getCapabilities().map(cap => cap.specs || [])
        ))
        this.specs = [...specs, ...capSpecs]
        this.isRunningTests = false
    }
 
    async watch () {
        /**
         * listen on spec changes and rerun specific spec file
         */
        chokidar.watch(this.specs, { ignoreInitial: true })
            .on('add', this.getFileListener())
            .on('change', this.getFileListener())
 
        /**
         * listen on filesToWatch changes an rerun complete suite
         */
        const { filesToWatch } = this.launcher.configParser.getConfig()
        if (filesToWatch.length) {
            chokidar.watch(filesToWatch, { ignoreInitial: true })
                .on('add', this.getFileListener(false))
                .on('change', this.getFileListener(false))
        }
 
        /**
         * run initial test suite
         */
        await this.launcher.run()
 
        /**
         * clean interface once all worker finish
         */
        const workers = this.getWorkers()
        Object.values(workers).forEach((worker) => worker.on('exit', () => {
            /**
             * check if all workers have finished
             */
            if (Object.values(workers).find((w) => w.isBusy)) {
                return
            }
 
            this.launcher.interface.finalise()
        }))
    }
 
    /**
     * return file listener callback that calls `run` method
     * @param  {Boolean}  [passOnFile=true]  if true pass on file change as parameter
     * @return {Function}                    chokidar event callback
     */
    getFileListener (passOnFile = true) {
        return (spec) => this.run(
            Object.assign({}, this.argv, passOnFile ? { spec } : {})
        )
    }
 
    /**
     * helper method to get workers from worker pool of wdio runner
     * @param  {Function} pickBy             filter by property value (see lodash.pickBy)
     * @param  {Boolean}  includeBusyWorker  don't filter out busy worker (default: false)
     * @return {Object}                      Object with workers, e.g. {'0-0': { ... }}
     */
    getWorkers (pickByFn, includeBusyWorker) {
        let workers = this.launcher.runner.workerPool
 
        if (typeof pickByFn === 'function') {
            workers = pickBy(workers, pickByFn)
        }
 
        /**
         * filter out busy workers, only skip if explicitely desired
         */
        if (!includeBusyWorker) {
            workers = pickBy(workers, (worker) => !worker.isBusy)
        }
 
        return workers
    }
 
    /**
     * run workers with params
     * @param  {Object} [params={}]  parameters to run the worker with
     */
    run (params = {}) {
        const workers = this.getWorkers(
            params.spec ? (worker) => worker.specs.includes(params.spec) : null)
 
        /**
         * don't do anything if no worker was found
         */
        if (Object.keys(workers).length === 0) {
            return
        }
 
        /**
         * update total worker count interface
         * ToDo: this should have a cleaner solution
         */
        this.launcher.interface.totalWorkerCnt = Object.entries(workers).length
 
        /**
         * clean up interface
         */
        this.cleanUp()
 
        /**
         * trigger new run for non busy worker
         */
        for (const [, worker] of Object.entries(workers)) {
            const { cid, caps, specs, sessionId } = worker
            const argv = Object.assign({ sessionId }, params)
            worker.postMessage('run', argv)
            this.launcher.interface.emit('job:start', { cid, caps, specs })
        }
    }
 
    cleanUp () {
        this.launcher.interface.setup()
    }
}