forked from karma-runner/karma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.js
66 lines (58 loc) · 1.72 KB
/
world.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
const fs = require('fs')
const vm = require('vm')
const path = require('path')
const hasher = require('crypto').createHash
const mkdirp = require('mkdirp')
const _ = require('lodash')
const { setWorldConstructor } = require('cucumber')
const Proxy = require('./proxy')
class World {
constructor () {
this.proxy = new Proxy()
this.template = _.template(`process.env.CHROME_BIN = require('puppeteer').executablePath(); module.exports = function (config) {\n config.set(\n <%= content %>\n );\n};`)
this.configFile = {
singleRun: true,
reporters: ['dots'],
frameworks: ['jasmine'],
basePath: __dirname,
colors: false,
__dirname: __dirname,
_resolve: (name) => path.resolve(__dirname, '..', 'support', name)
}
this.lastRun = {
error: null,
stdout: '',
stderr: ''
}
}
addConfigContent (content) {
if (content == null) {
content = ''
}
return vm.runInNewContext(content, this.configFile)
}
writeConfigFile (dir, file, done) {
return mkdirp(dir, 0x1ed, (err) => {
let content, hash
if (err) {
return done(err)
}
delete this.configFile.__dirname
content = this.generateJS(this.configFile)
hash = hasher('md5').update(content + Math.random()).digest('hex')
fs.writeFile(path.join(dir, hash + '.' + file), content, (err) => {
done(err, hash)
})
})
}
generateJS (config) {
return this.template({
content: JSON.stringify(Object.assign({}, config, {
customLaunchers: Object.assign({
ChromeHeadlessNoSandbox: { base: 'ChromeHeadless', flags: ['--no-sandbox'] }
}, config.customLaunchers)
}))
})
}
}
setWorldConstructor(World)