-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathtestWithSpectron.js
76 lines (74 loc) · 2.27 KB
/
testWithSpectron.js
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
const {execa} = require('execa')
const electronPath = require('electron')
const merge = require('lodash.merge')
const portfinder = require('portfinder')
portfinder.basePort = 9515
module.exports = (spectron, options = {}) =>
// eslint-disable-next-line no-async-promise-executor
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 spectron.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('SIGKILL')
// Close spectron
if (app && app.isRunning()) return app.stop()
}
})
}
})
})