-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathqunit.js
More file actions
209 lines (182 loc) · 6.16 KB
/
qunit.js
File metadata and controls
209 lines (182 loc) · 6.16 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* grunt-contrib-qunit
* http://gruntjs.com/
*
* Copyright (c) 2013 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
// Nodejs libs.
var path = require('path');
// External lib.
var phantomjs = require('grunt-lib-phantomjs').init(grunt);
// Keep track of the last-started module, test and status.
var options, currentModule, currentTest, status;
// Keep track of the last-started test(s).
var unfinished = {};
// Get an asset file, local to the root of the project.
var asset = path.join.bind(null, __dirname, '..');
// Allow an error message to retain its color when split across multiple lines.
var formatMessage = function(str) {
return String(str).split('\n').map(function(s) { return s.magenta; }).join('\n');
};
// If options.force then log an error, otherwise exit with a warning
var warnUnlessForced = function (message) {
if (options && options.force) {
grunt.log.error(message);
} else {
grunt.warn(message);
}
};
// Keep track of failed assertions for pretty-printing.
var failedAssertions = [];
var logFailedAssertions = function() {
var assertion;
// Print each assertion error.
while (assertion = failedAssertions.shift()) {
grunt.verbose.or.error(assertion.testName);
grunt.log.error('Message: ' + formatMessage(assertion.message));
if (assertion.actual !== assertion.expected) {
grunt.log.error('Actual: ' + formatMessage(assertion.actual));
grunt.log.error('Expected: ' + formatMessage(assertion.expected));
}
if (assertion.source) {
grunt.log.error(assertion.source.replace(/ {4}(at)/g, ' $1'));
}
grunt.log.writeln();
}
};
// QUnit hooks.
phantomjs.on('qunit.moduleStart', function(name) {
unfinished[name] = true;
currentModule = name;
});
phantomjs.on('qunit.moduleDone', function(name/*, failed, passed, total*/) {
delete unfinished[name];
});
phantomjs.on('qunit.log', function(result, actual, expected, message, source) {
if (!result) {
failedAssertions.push({
actual: actual, expected: expected, message: message, source: source,
testName: currentTest
});
}
});
phantomjs.on('qunit.testStart', function(name) {
currentTest = (currentModule ? currentModule + ' - ' : '') + name;
grunt.verbose.write(currentTest + '...');
});
phantomjs.on('qunit.testDone', function(name, failed/*, passed, total*/) {
// Log errors if necessary, otherwise success.
if (failed > 0) {
// list assertions
if (grunt.option('verbose')) {
grunt.log.error();
logFailedAssertions();
} else {
grunt.log.write('F'.red);
}
} else {
grunt.verbose.ok().or.write('.');
}
});
phantomjs.on('qunit.done', function(failed, passed, total, duration) {
phantomjs.halt();
status.failed += failed;
status.passed += passed;
status.total += total;
status.duration += duration;
// Print assertion errors here, if verbose mode is disabled.
if (!grunt.option('verbose')) {
if (failed > 0) {
grunt.log.writeln();
logFailedAssertions();
} else if (total === 0) {
warnUnlessForced('0/0 assertions ran (' + duration + 'ms)');
} else {
grunt.log.ok();
}
}
});
// Re-broadcast qunit events on grunt.event.
phantomjs.on('qunit.*', function() {
var args = [this.event].concat(grunt.util.toArray(arguments));
grunt.event.emit.apply(grunt.event, args);
});
// Built-in error handlers.
phantomjs.on('fail.load', function(url) {
phantomjs.halt();
grunt.verbose.write('Running PhantomJS...').or.write('...');
grunt.event.emit('qunit.fail.load', url);
grunt.log.error('PhantomJS unable to load "' + url + '" URI.');
status.failed += 1;
status.total += 1;
});
phantomjs.on('fail.timeout', function() {
phantomjs.halt();
grunt.log.writeln();
grunt.event.emit('qunit.fail.timeout');
grunt.log.error('PhantomJS timed out, possibly due to a missing QUnit start() call.');
status.failed += 1;
status.total += 1;
});
// Pass-through console.log statements.
phantomjs.on('console', console.log.bind(console));
grunt.registerMultiTask('qunit', 'Run QUnit unit tests in a headless PhantomJS instance.', function() {
// Merge task-specific and/or target-specific options with these defaults.
options = this.options({
// Default PhantomJS timeout.
timeout: 5000,
// QUnit-PhantomJS bridge file to be injected.
inject: asset('phantomjs/bridge.js'),
// Explicit non-file URLs to test.
urls: [],
force: false
});
// Combine any specified URLs with src files.
var urls = options.urls.concat(this.filesSrc);
// This task is asynchronous.
var done = this.async();
// Reset status.
status = {failed: 0, passed: 0, total: 0, duration: 0};
// Process each filepath in-order.
grunt.util.async.forEachSeries(urls, function(url, next) {
var basename = path.basename(url);
grunt.verbose.subhead('Testing ' + url + ' ').or.write('Testing ' + url + ' ');
// Reset current module.
currentModule = null;
// Launch PhantomJS.
grunt.event.emit('qunit.spawn', url);
phantomjs.spawn(url, {
// Additional PhantomJS options.
options: options,
// Do stuff when done.
done: function(err) {
if (err) {
// If there was an error, abort the series.
done();
} else {
// Otherwise, process next url.
next();
}
},
});
},
// All tests have been run.
function() {
// Log results.
if (status.failed > 0) {
warnUnlessForced(status.failed + '/' + status.total +
' assertions failed (' + status.duration + 'ms)');
} else if (status.total === 0) {
warnUnlessForced('0/0 assertions ran (' + status.duration + 'ms)');
} else {
grunt.verbose.writeln();
grunt.log.ok(status.total + ' assertions passed (' + status.duration + 'ms)');
}
// All done!
done();
});
});
};