forked from shellscape/mocha-chrome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
164 lines (122 loc) · 3.8 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
154
155
156
157
158
159
160
161
162
163
164
'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');
process.on('unhandledRejection', error => {
// this should always output to console.error, regardless of loglevel
console.error('Promise Rejection: ', error);
});
class MochaChrome {
constructor (options) {
options = deepAssign({
chromeFlags: [],
loadTimeout: 1000,
logLevel: 'error',
mocha: {
reporter: 'spec',
ui: 'bdd',
useColors: true
}
}, options);
log.setDefaultLevel('error');
log.setLevel(options.logLevel);
const bus = new EventBus(log);
if (!options.url) {
this.fail('`options.url` must be specified to run tests');
}
bus.on('ready', content => {
this.client.Runtime.evaluate({ expression: `mocha.setup({ reporter: 'spec' })`});
});
bus.on('mocha', content => {
process.stdout.write(content);
});
bus.on('width', content => {
const columns = parseInt(process.env.COLUMNS || process.stdout.columns) * .75 | 0;
const expression = `Mocha.reporters.Base.window.width = ${columns};`;
this.client.Runtime.evaluate({ expression });
});
bus.on('config', content => {
const config = JSON.stringify(options.mocha);
const expression = `mocha.setup(${config})`;
this.client.Runtime.evaluate({ expression });
});
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');
}
});
bus.on('ended', stats => {
this.ended = true;
this.exit(stats.failures);
});
bus.on('resourceFailed', data => {
this.loadError = true;
});
this.bus = bus;
this.options = options;
this.loadError = false;
}
async connect () {
const instance = await createInstance(log, this.options);
const client = await connectClient(instance, log, this.options);
const { DOMStorage, Network, Runtime } = client;
if (!client) {
fail('CDP Client could not connect');
return;
}
this.bus.watch(DOMStorage);
chromeOut(log, Runtime);
network(this.bus, log, Network);
this.client = client;
this.instance = instance;
}
async 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(async () => {
if (this.closed) {
return;
}
const expression = '(function () { return !!window.mocha; })()';
let res = await this.client.Runtime.evaluate({ expression });
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);
});
await 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);
}
async exit (code) {
this.closed = true;
await this.client.close();
await this.instance.kill();
this.bus.emit('exit', 1);
}
}
module.exports = MochaChrome;