forked from elastic/apm-agent-rum-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-utils.js
172 lines (161 loc) · 4.48 KB
/
test-utils.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
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
/**
* MIT License
*
* Copyright (c) 2017-present, Elasticsearch BV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const { Server } = require('karma')
const { default: Launcher } = require('@wdio/cli')
const sauceConnectLauncher = require('sauce-connect-launcher')
const JasmineRunner = require('jasmine')
function walkSync(dir, filter, filelist) {
var files = fs.readdirSync(dir)
filelist = filelist || []
files.forEach(function (file) {
var filename = path.join(dir, file)
var stat = fs.statSync(filename)
if (stat.isDirectory()) {
filelist = walkSync(filename, filter, filelist)
} else {
if (typeof filter.test === 'function') {
if (filter.test(filename)) {
filelist.push(filename)
}
} else {
filelist.push(filename)
}
}
})
return filelist
}
function buildE2eBundles(basePath, callback) {
var cb =
callback ||
function (err) {
if (err) {
process.exit(2)
}
}
var fileList = walkSync(basePath, /webpack\.config\.js$/)
fileList = fileList.map(function (file) {
return path.relative(__dirname, file)
})
var configs = fileList
.map(f => {
return require(f)
})
.reduce((acc, cfg) => {
if (cfg.length) {
return acc.concat(cfg)
} else {
acc.push(cfg)
return acc
}
}, [])
console.log('Webpack config files: \n', fileList.join('\n'), '\n')
webpack(configs, (err, stats) => {
if (err) {
console.error(err)
return cb(err)
}
const info = stats.toJson('minimal')
if (stats.hasErrors()) {
console.error('There were errors while building')
info.errors.forEach(err => console.error(err))
cb(info.errors)
} else {
console.log(
stats.toString({
colors: true,
chunks: false,
assets: false,
modules: false,
warnings: false
})
)
cb()
}
})
}
function runSauceConnect(config, callback) {
return sauceConnectLauncher(config, (err, sauceConnectProcess) => {
if (err) {
console.error('Sauce connect Error', err)
return process.exit(1)
} else {
console.log('Sauce connect ready')
callback(sauceConnectProcess)
}
})
}
function runKarma(configFile) {
const server = new Server({ configFile, singleRun: true }, exitCode =>
process.exit(exitCode)
)
server.start()
}
function runE2eTests(configFilePath) {
const wdio = new Launcher(configFilePath, {})
wdio.run().then(
function (code) {
process.stdin.pause()
process.nextTick(() => process.exit(code))
},
function (error) {
console.error('Launcher failed to start the test', error)
process.stdin.pause()
process.nextTick(() => process.exit())
}
)
}
function runJasmine(specDir, cb) {
const jrunner = new JasmineRunner()
/**
* spec dir is a relative directory path from the current working dir
*/
jrunner.loadConfig({
spec_dir: specDir,
spec_files: ['*.spec.js'],
helpers: ['*.helper.js']
})
jrunner.onComplete(passed => {
if (!passed) {
const err = new Error('Jasmine tests failed')
// The stack is not useful in this context.
err.stack = ''
cb(err)
} else {
cb()
}
})
jrunner.execute()
}
module.exports = {
buildE2eBundles,
runE2eTests,
runKarma,
runJasmine,
runSauceConnect,
dirWalkSync: walkSync
}