-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
87 lines (71 loc) · 2.6 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
const path = require('path');
const chalk = require('chalk');
const childProcess = require('child_process');
const phantomjs = require('phantomjs-prebuilt');
const binPath = phantomjs.path;
const phantomjsRunnerDir = path.dirname(require.resolve('qunit-phantomjs-runner'));
const isUrl = uri => uri.match(/^http(s?):/) !== null;
module.exports = (uri, options, callback) => {
const opt = options || {};
const cb = callback || (() => {});
let runner = path.join(phantomjsRunnerDir, 'runner-json.js');
const testUri = isUrl(uri) ? uri : `file:///${path.resolve(uri).replace(/\\/g, '/')}`;
let childArgs = [];
let proc;
if (opt.verbose) {
runner = path.join(phantomjsRunnerDir, 'runner-list.js');
} else if (opt.customRunner) {
// A custom phantomjs runner can be used to have more control
// over phantomjs configuration or to customize phantomjs hooks.
runner = opt.customRunner;
}
if (opt['phantomjs-options'] && opt['phantomjs-options'].length) {
if (Array.isArray(opt['phantomjs-options'])) {
childArgs = childArgs.concat(opt['phantomjs-options']);
} else {
childArgs.push(opt['phantomjs-options']);
}
}
childArgs.push(
runner,
testUri
);
if (opt.timeout) {
childArgs.push(opt.timeout);
}
if (opt.page) {
// Push default timeout value unless specified otherwise
if (!opt.timeout) {
childArgs.push(5);
}
childArgs.push(JSON.stringify(opt.page));
}
console.log(`Testing ${chalk.blue(testUri)}`);
// phantomjs [phantomjs-options] runner testuri [timeout [page]]
proc = childProcess.spawn(binPath, childArgs);
proc.stdout.on('data', data => {
let out;
let test;
let message;
const line = data.toString().trim();
try {
out = JSON.parse(line);
} catch (err) {
console.log(line);
return;
}
if (out.exceptions) {
for (test in out.exceptions) {
console.log(`\n${chalk.red('Test failed')}: ${chalk.red(test)}: \n${out.exceptions[test].join('\n ')}`);
}
}
if (out.result) {
message = `Took ${out.result.runtime} ms to run ${out.result.total} tests. ${out.result.passed} passed, ${out.result.failed} failed.`;
console.log(out.result.failed > 0 ? chalk.red(message) : chalk.green(message));
}
});
proc.stderr.on('data', data => {
console.log(data.toString().trim());
});
proc.on('close', code => cb(code));
};