Skip to content

Commit 7e7222e

Browse files
committed
Break apart test options, runner and display
For jsdom#711, the test runner will execute in a web worker, but test results will be displayed in the parent page or a shell. So, it is convenient to break the options parsing, test running, and results display into their own modules for reuse by other runners.
1 parent 049a523 commit 7e7222e

File tree

4 files changed

+250
-198
lines changed

4 files changed

+250
-198
lines changed

test/runner

+3-198
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,8 @@ var nodeunit = require('nodeunit'),
55
path = require('path'),
66
AssertionError = require('assert').AssertionError;
77

8-
var optimist = require('optimist')
9-
.usage('Run the jsdom test suite')
10-
.alias('s', 'suites')
11-
.string('s')
12-
.describe('s', 'suites that you want to run. ie: -s level1/core,1/html,html')
13-
.alias('f', 'fail-fast')
14-
.describe('f', 'stop on the first failed test')
15-
.alias('h', 'help')
16-
.describe('h', 'show the help')
17-
.alias('t', 'tests')
18-
.describe('t', 'choose the test cases to run. ie: -t jquery')
19-
.alias('d', 'debug')
20-
.describe('d', 'run in node\'s interactive debugger mode')
21-
.alias('p', 'parser')
22-
.describe('p', 'the HTML parser to use (e.g. html5); default is htmlparser')
23-
.alias('v', 'verbose')
24-
.describe('v', 'show all tests that are being run');
8+
var argv = require('./runner-options');
259

26-
var argv = optimist.argv;
27-
if (argv.help) {
28-
optimist.showHelp();
29-
process.exit();
30-
}
31-
32-
33-
var totalTests = 0;
34-
var failedTests = 0;
35-
var passedTests = 0;
36-
var modules = {};
37-
var currentModule = "";
38-
var moduleIndex = 0;
39-
var start = new Date().getTime();
4010
var fileFilter = [];
4111
var testFilter = [];
4212

@@ -51,28 +21,6 @@ if (argv.tests) {
5121
testFilter = argv.tests.replace(/\s/g, '').split(',');
5222
}
5323

54-
var assert = require('nodeunit/lib/assert');
55-
require('nodeunit/lib/assert').equal = function equal(actual, expected, message) {
56-
if (actual != expected) {
57-
if (actual && actual.nodeType) {
58-
actual = actual.toString();
59-
}
60-
61-
if (expected && expected.nodeType) {
62-
expected = expected.toString();
63-
}
64-
65-
assert.fail(actual, expected, message, '==', assert.equal);
66-
}
67-
};
68-
69-
assert.domSame = function(actual, expected, message) {
70-
if(expected != actual) {
71-
assert.equal(expected.nodeType, actual.nodeType);
72-
assert.equal(expected.nodeValue, actual.nodeValue);
73-
}
74-
};
75-
7624
var files = [
7725
"level1/core.js",
7826
"level1/html.js",
@@ -155,148 +103,5 @@ if (argv.parser) {
155103
browser.setDefaultParser(argv.parser);
156104
}
157105

158-
nodeunit.runModules(modulesToRun, {
159-
moduleStart: function (name) {
160-
currentModule = name.replace('.js', '');
161-
console.log("running", name, currentModule);
162-
modules[currentModule] = {
163-
total : 0,
164-
fail : 0,
165-
pass : 0
166-
};
167-
moduleIndex++;
168-
},
169-
moduleDone: function (name, assertions) {
170-
if (argv['verbose']) {
171-
console.log(' ');
172-
}
173-
},
174-
testStart: function (test) {
175-
modules[currentModule].total++;
176-
if (argv['verbose']) {
177-
process.stdout.write(' ' + test[0] + ' ...');
178-
}
179-
},
180-
testDone: function (test, assertions) {
181-
if (argv['verbose']) {
182-
console.log(' done');
183-
}
184-
totalTests++;
185-
if (!assertions.failures()) {
186-
passedTests++;
187-
modules[currentModule].pass++;
188-
}
189-
else {
190-
failedTests++;
191-
modules[currentModule].fail++;
192-
193-
console.log('✖ ' + currentModule + '/' + test);
194-
assertions.forEach(function (a) {
195-
if (a.failed()) {
196-
if (a.error instanceof AssertionError) {
197-
a = nodeunit.utils.betterErrors(a);
198-
if (a.message) {
199-
console.log(
200-
'Assertion Message: ' + assertion_message(a.message) + '\n' +
201-
'expected:', a.error.expected, 'got:', a.error.actual
202-
);
203-
}
204-
} else {
205-
if (a.error.expected || a.error.actual) {
206-
console.log('\nERROR', a.error.expected, 'vs', a.error.actual, '\n');
207-
}
208-
209-
console.log(a.error.message, a.error.stack, (new Error()).stack);
210-
}
211-
} else {
212-
console.log(a.message);
213-
}
214-
});
215-
216-
if (argv['fail-fast']) {
217-
process.exit();
218-
}
219-
}
220-
},
221-
done: function (assertions) {
222-
var end = new Date().getTime();
223-
var duration = end - start;
224-
var maxWidths = {
225-
name : 0,
226-
ratio : 0,
227-
percent : 4
228-
};
229-
var width = 0;
230-
var keys = Object.keys(modules);
231-
232-
var calculateMax = function(name, value) {
233-
if (!maxWidths[name] || value.length > maxWidths[name]) {
234-
maxWidths[name] = value.length;
235-
}
236-
237-
width = 2;
238-
Object.keys(maxWidths).forEach(function(v) {
239-
width += maxWidths[v] + 2;
240-
});
241-
}
242-
243-
var pad = function(name, value, rightJustified) {
244-
var ret = '';
245-
var padding = '';
246-
247-
var amount = maxWidths[name] - value.length;
248-
while(amount--) {
249-
padding += " ";
250-
}
251-
252-
if (rightJustified) {
253-
return ' ' + padding + value + ' ';
254-
} else {
255-
return ' ' + value + padding + ' ';
256-
}
257-
}
258-
259-
// First pass, calculate the max widths
260-
keys.forEach(function(v) {
261-
var module = modules[v];
262-
var ratio = module.pass + '/' + module.total;
263-
var percentage = Math.floor((module.pass/module.total)*100) + '%';
264-
modules[v].ratio = ratio;
265-
modules[v].percentage = percentage;
266-
calculateMax('name', v);
267-
calculateMax('ratio', ratio);
268-
calculateMax('percentage', percentage);
269-
});
270-
271-
var caps = '';
272-
var gen = width;
273-
274-
while(gen--) {
275-
caps += '-';
276-
}
277-
278-
console.log('');
279-
Object.keys(modules).forEach(function(v) {
280-
var module = modules[v];
281-
process.stdout.write(pad('name', v, false));
282-
process.stdout.write(pad('ratio', module.ratio, true));
283-
process.stdout.write(pad('percentage', module.percentage, true));
284-
process.stdout.write('\n');
285-
});
286-
console.log(caps);
287-
var ratio = failedTests + '/' + totalTests;
288-
var percent = 0;
289-
if (totalTests === 0) {
290-
percent = '100%';
291-
} else {
292-
percent = Math.floor((passedTests/totalTests)*100) + '%';
293-
}
294-
console.log('TOTALS: %s failed; %s success', ratio, percent);
295-
console.log('TIME: %dms', duration);
296-
297-
if (passedTests !== totalTests) {
298-
process.exit(1);
299-
}
300-
301-
}
302-
});
106+
var runner = require('./runner-core')(modulesToRun);
107+
require('./runner-display')(runner, argv);

test/runner-core.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var EventEmitter = require('events').EventEmitter;
2+
var nodeunit = require('nodeunit');
3+
4+
var assert = require('nodeunit/lib/assert');
5+
6+
assert.equal = function equal(actual, expected, message) {
7+
if (actual != expected) {
8+
if (actual && actual.nodeType) {
9+
actual = actual.toString();
10+
}
11+
12+
if (expected && expected.nodeType) {
13+
expected = expected.toString();
14+
}
15+
16+
assert.fail(actual, expected, message, '==', assert.equal);
17+
}
18+
};
19+
20+
assert.domSame = function(actual, expected, message) {
21+
if(expected != actual) {
22+
assert.equal(expected.nodeType, actual.nodeType);
23+
assert.equal(expected.nodeValue, actual.nodeValue);
24+
}
25+
};
26+
27+
module.exports = function runModules(toRun) {
28+
var emitter = new EventEmitter();
29+
30+
process.nextTick(function () {
31+
nodeunit.runModules(toRun, {
32+
moduleStart: function (name) {
33+
emitter.emit('moduleStart', name);
34+
},
35+
moduleDone: function (name, assertions) {
36+
emitter.emit('moduleDone', name, assertions);
37+
},
38+
testStart: function (name) {
39+
emitter.emit('testStart', name);
40+
},
41+
testReady: function (name) {
42+
emitter.emit('testReady', name);
43+
},
44+
testDone: function (test, assertions) {
45+
emitter.emit('testDone', test, assertions);
46+
},
47+
log: function (assertion) {
48+
emitter.emit('log', assertion);
49+
},
50+
done: function (assertions) {
51+
emitter.emit('done', assertions);
52+
}
53+
});
54+
});
55+
56+
return emitter;
57+
};

0 commit comments

Comments
 (0)