-
Notifications
You must be signed in to change notification settings - Fork 26
/
phantom.js
174 lines (148 loc) · 5.27 KB
/
phantom.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
var COVERAGE = !!process.env["npm_config_coverage"];
var PATH = require("path");
var spawn = require("child_process").spawn;
var Promise = require("bluebird");
var phantom = require("phantom-wd");
var joey = require("joey");
var Apps = require("q-io/http-apps");
if (COVERAGE) {
var IGNORE_RE = /spec|packages/;
var FS = require("q-io/fs");
var istanbul = require("istanbul");
var instrumenter = new istanbul.Instrumenter();
var fileTree = function (path) {
return Apps.FileTree(path, {
// use a custom file reader to instrument the code
file: function (request, path, contentType, fs) {
if (path.match(/.js$/) && !path.match(IGNORE_RE)) {
// instrument JS files
return FS.read(path, "r", "utf8").then(function (original) {
var response = Promise.defer();
instrumenter.instrument(original, path, function (err, instrumented) {
if (err) {
response.reject(err);
return;
}
response.resolve({
status: 200,
headers: {
"content-type": "application/javascript",
"content-length": instrumented.length
},
body: [instrumented],
file: path
});
});
return response.promise;
});
}
// otherwise just serve the file
return Apps.file(request, path, contentType, fs);
}
});
};
} else {
var fileTree = Apps.FileTree;
}
var TESTS_FAILED = {};
var POLL_TIME = 250;
var server = joey
.error(true)
.app(fileTree(PATH.resolve(__dirname, "..")))
.server();
server.listen(0).done();
var testPagePort = server.node.address().port;
var testPageUrl = "http://127.0.0.1:" + testPagePort + "/spec/run.html";
console.log("Test page at " + testPageUrl);
// Start PhantomJS webdriver
phantom()
.then(function (browser) {
return browser.get(testPageUrl)
.then(function () {
var done = Promise.defer();
var poll = function() {
browser.execute("return typeof jsApiReporter !== 'undefined' ? jsApiReporter.finished : false").then(function (isFinished) {
if (isFinished) {
done.resolve();
} else {
setTimeout(poll, POLL_TIME);
}
}, done.reject);
};
poll();
return done.promise;
})
.then(function () {
return browser.execute("return [jsApiReporter.suites(), jsApiReporter.results()]");
})
.spread(function (suites, results) {
var info = log(suites, results);
if (info.failures.length) {
console.log("\nFailures:\n");
console.log(info.failures.join("\n\n"));
}
var msg = '';
msg += info.specsCount + ' test' + ((info.specsCount === 1) ? '' : 's') + ', ';
msg += info.totalCount + ' assertion' + ((info.totalCount === 1) ? '' : 's') + ', ';
msg += info.failedCount + ' failure' + ((info.failedCount === 1) ? '' : 's');
console.log();
console.log(msg);
if (info.failures.length) {
throw TESTS_FAILED;
}
})
.then(function () {
if (!COVERAGE) {
return;
}
return browser.execute("return window.__coverage__")
.then(function (coverage) {
var reporter = istanbul.Report.create("lcov");
var collector = new istanbul.Collector();
collector.add(coverage);
console.log("Writing coverage reports.");
reporter.writeReport(collector);
});
})
.finally(function () {
server.stop();
})
.finally(function () {
return browser.quit();
});
})
.fail(function (err) {
if (err === TESTS_FAILED) {
process.exit(1);
}
throw err;
});
function log(suites, results, name, info) {
name = name || "";
info = info || {specsCount: 0, totalCount: 0, failedCount: 0, failures: []};
for (var i = 0, len = suites.length; i < len; i++) {
var suite = suites[i];
if (suite.type === "spec") {
var result = results[suite.id];
info.specsCount++;
info.totalCount += result.messages.length;
if (result.result === "passed") {
process.stdout.write(".");
} else {
process.stdout.write("F");
var msg = suite.name + "\n";
for (var j = 0; j < result.messages.length; j++) {
var message = result.messages[j];
if (message.passed_) continue;
info.failedCount++;
msg += "\t" + message.message + "\n";
}
info.failures.push(msg);
}
}
if (suite.children.length) {
log(suite.children, results, name + suite.name + " ", info);
}
}
return info;
}