-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
176 lines (137 loc) · 4.37 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
165
166
167
168
169
170
171
172
173
174
175
176
const deepAssign = require('deep-assign');
const log = require('loglevel');
const unmirror = require('chrome-unmirror');
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');
process.on('unhandledRejection', (error) => {
const { error: stderr } = console;
// this should always output to console.error, regardless of loglevel
stderr('Promise Rejection: ', error);
});
class MochaChrome {
constructor(options) {
// eslint-disable-next-line no-param-reassign
options = deepAssign(
{
chromeFlags: [],
chromeLauncher: {},
loadTimeout: 1000,
logLevel: 'error',
ignoreExceptions: false,
ignoreConsole: false,
ignoreResourceErrors: false,
mocha: {
reporter: 'spec',
ui: 'bdd',
colors: 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 */) => {
// eslint-disable-next-line no-bitwise
const columns = (parseInt(process.env.COLUMNS || process.stdout.columns, 10) * 0.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', (resource) => {
this.failedResources.push(resource.url);
});
this.bus = bus;
this.options = options;
this.failedResources = [];
}
async connect() {
const instance = await createInstance(log, this.options);
const client = await connectClient(instance, log, this.options);
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;
}
async run() {
this.client.Page.loadEventFired(() => {
if (this.closed) {
return;
}
if (this.failedResources.length && !this.options.ignoreResourceErrors) {
this.fail(`The following resources failed to load on the page:
${this.failedResources.join('\n')}
You may suprress these failures with --ignore-resource-errors`);
return;
}
setTimeout(async () => {
if (this.closed) {
return;
}
const expression = '(function () { return !!window.mocha; })()';
const 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;