All files / vue-cli-plugin-electron-builder/lib testWithSpectron.js

84.85% Statements 28/33
76.47% Branches 13/17
60% Functions 3/5
87.5% Lines 28/32

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 771x 1x 1x 1x 1x   1x   1x 10x 10x 10x   10x                     10x     10x 21x 21x 21x 21x 21x   10x   21x   10x   10x 9x                           9x 9x 8x 8x     10x                              
const execa = require('execa')
const Application = require('spectron').Application
const electronPath = require('electron')
const merge = require('lodash.merge')
const portfinder = require('portfinder')
 
portfinder.basePort = 9515
 
module.exports = (options = {}) =>
  new Promise(async (resolve, reject) => {
    let log = ''
    let outputDir = ''
    // Launch electron:serve in headless mode
    const child = execa(
      require.resolve('@vue/cli-service/bin/vue-cli-service'),
      ['electron:serve', '--headless', '--mode', options.mode || 'test'],
      {
        env: {
          ...process.env,
          NODE_ENV: !options.forceDev ? 'production' : 'development'
        }
      }
    )
    // Exit if electron:serve throws an error
    child.on('error', err => {
      reject(err)
    })
    child.stdout.on('data', async data => {
      data = data.toString()
      log += data
      const urlMatch = data.match(/\$WEBPACK_DEV_SERVER_URL=https?:\/\/[^/]+\//)
      const outputDirMatch = data.match(/\$outputDir=\b.*\b/)
      if (outputDirMatch) {
        // Record output dir
        outputDir = outputDirMatch[0].split('=')[1]
      }
      if (urlMatch) {
        // Record url and launch spectron
        const url = urlMatch[0].split('=')[1]
        let app
        if (!options.noSpectron) {
          const spectronOptions = merge(
            {
              path: electronPath,
              args: [`${outputDir}`],
              env: {
                IS_TEST: true
              },
              //   Make sure tests do not interfere with each other
              port: await portfinder.getPortPromise()
            },
            // Apply user options
            options.spectronOptions
          )
          // Launch app with spectron
          app = new Application(spectronOptions)
          if (!options.noStart) {
            await app.start()
            await app.client.waitUntilWindowLoaded()
          }
        }
        resolve({
          stdout: log,
          url,
          app,
          stopServe: () => {
            //   Exit serve process
            child.stdin.write('close')
            child.kill()
            // Close spectron
            if (app && app.isRunning()) return app.stop()
          }
        })
      }
    })
  })