-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (123 loc) · 3.64 KB
/
index.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
'use strict'
const createInstance = require('./lib/instance')
const connectClient = require('./lib/client')
const EventBus = require('./lib/event-bus')
const chromeOut = require('./lib/chrome-out')
const network = require('./lib/network')
const deepAssign = require('deep-assign')
const log = require('loglevel')
const unmirror = require('chrome-unmirror')
class MochaChrome {
constructor (options) {
options = deepAssign({
chromeFlags: [],
loadTimeout: 1000,
logLevel: 'error',
ignoreExceptions: false,
ignoreConsole: false,
ignoreResourceErrors: false,
mocha: {
reporter: 'spec',
ui: 'bdd',
useColors: true
}
}, options)
log.setDefaultLevel('error')
log.setLevel(options.logLevel)
if (!options.url) {
this.fail('`options.url` must be specified to run tests')
}
this.options = options
this.loadError = false
this.bus = new EventBus(log)
this.listenBus()
}
listenBus () {
this.bus.on('width', content => {
const columns = parseInt(process.env.COLUMNS || process.stdout.columns) * 0.75 | 0
const expression = `Mocha.reporters.Base.window.width = ${columns};`
this.client.Runtime.evaluate({ expression })
})
this.bus.on('started', (tests) => {
this.started = true
log.info(`Test Run Started - Running ${tests} Tests\n`)
if (tests === 0) {
this.fail('mocha.run() was called with no tests')
}
})
this.bus.on('ended', stats => {
this.ended = true
this.exit(stats.failures)
})
this.bus.on('resourceFailed', data => {
this.loadError = true
})
}
connect () {
return new Promise((resolve, reject) => {
createInstance(log, this.options).then((instance) => {
connectClient(instance, log, this.options)
.then((client) => {
const {DOMStorage, Network, Runtime} = client
if (!client) {
this.fail('CDP Client could not connect')
return
}
this.bus.watch(DOMStorage)
chromeOut(log, this.options, Runtime)
network(this.bus, log, Network, this.options.ignoreResourceErrors)
this.client = client
this.instance = instance
resolve()
})
.catch(reject)
}).catch(reject)
})
}
run () {
this.client.Page.loadEventFired(() => {
if (this.closed) {
return
}
if (this.loadError) {
this.fail('Failed to load the page. Check the url: ' + this.options.url)
return
}
setTimeout(() => {
if (this.closed) {
return
}
const expression = '(function () { return !!window.mocha; })()'
this.client.Runtime.evaluate({expression})
.then((res) => {
if (!unmirror(res.result)) {
this.fail(`mocha was not found in the page within ${this.options.loadTimeout}ms of the page loading.`)
}
if (!this.started) {
this.fail(`mocha.run() was not called within ${this.options.loadTimeout}ms of the page loading.`)
}
})
}, this.options.loadTimeout)
})
return this.client.Page.navigate({url: this.options.url})
}
on (name, fn) {
this.bus.on(name, fn)
}
fail (message) {
log.error('Mocha-Chrome Failed:', message || '')
if (this.bus) {
this.bus.emit('failure', message)
}
this.exit(1)
}
exit (code) {
this.closed = true
this.client.close()
.then(() => this.instance.kill())
.then(() => {
this.bus.emit('exit', code)
})
}
}
module.exports = MochaChrome