UNPKG

3.89 kBJavaScriptView Raw
1/*!
2 * Nodeunit
3 * Copyright (c) 2010 Caolan McMahon
4 * MIT Licensed
5 */
6
7/**
8 * Module dependencies
9 */
10
11var nodeunit = require('../nodeunit'),
12 utils = require('../utils'),
13 fs = require('fs'),
14 track = require('../track'),
15 path = require('path'),
16 AssertionError = require('../assert').AssertionError;
17
18/**
19 * Reporter info string
20 */
21
22exports.info = "Verbose tests reporter"
23
24
25/**
26 * Run all tests within each module, reporting the results to the command-line.
27 *
28 * @param {Array} files
29 * @api public
30 */
31
32exports.run = function (files, options, callback) {
33
34 if (!options) {
35 // load default options
36 var content = fs.readFileSync(
37 __dirname + '/../../bin/nodeunit.json', 'utf8'
38 );
39 options = JSON.parse(content);
40 }
41
42 var error = function (str) {
43 return options.error_prefix + str + options.error_suffix;
44 };
45 var ok = function (str) {
46 return options.ok_prefix + str + options.ok_suffix;
47 };
48 var bold = function (str) {
49 return options.bold_prefix + str + options.bold_suffix;
50 };
51 var assertion_message = function (str) {
52 return options.assertion_prefix + str + options.assertion_suffix;
53 };
54 var pass_indicator = process.platform === 'win32' ? '\u221A' : '✔';
55 var fail_indicator = process.platform === 'win32' ? '\u00D7' : '✖';
56
57 var start = new Date().getTime();
58 var paths = files.map(function (p) {
59 return path.resolve(p);
60 });
61 var tracker = track.createTracker(function (tracker) {
62 if (tracker.unfinished()) {
63 console.log('');
64 console.log(error(bold(
65 'FAILURES: Undone tests (or their setups/teardowns): '
66 )));
67 var names = tracker.names();
68 for (var i = 0; i < names.length; i += 1) {
69 console.log('- ' + names[i]);
70 }
71 console.log('');
72 console.log('To fix this, make sure all tests call test.done()');
73 process.reallyExit(tracker.unfinished());
74 }
75 });
76
77 nodeunit.runFiles(paths, {
78 testspec: options.testspec,
79 testFullSpec: options.testFullSpec,
80 moduleStart: function (name) {
81 console.log('\n' + bold(name));
82 },
83 testDone: function (name, assertions) {
84 tracker.remove(name);
85
86 if (!assertions.failures()) {
87 console.log(pass_indicator + ' ' + name);
88 }
89 else {
90 console.log(error(fail_indicator + ' ' + name));
91 }
92 // verbose so print everything
93 assertions.forEach(function (a) {
94 if (a.failed()) {
95 console.log(error(' ' + fail_indicator + ' ' + a.message));
96 a = utils.betterErrors(a);
97 console.log(' ' + a.error.stack);
98 }
99 else {
100 console.log(' ' + pass_indicator + ' ' + a.message);
101 }
102 });
103 },
104 done: function (assertions, end) {
105 var end = end || new Date().getTime();
106 var duration = end - start;
107 if (assertions.failures()) {
108 console.log(
109 '\n' + bold(error('FAILURES: ')) + assertions.failures() +
110 '/' + assertions.length + ' assertions failed (' +
111 assertions.duration + 'ms)'
112 );
113 }
114 else {
115 console.log(
116 '\n' + bold(ok('OK: ')) + assertions.length +
117 ' assertions (' + assertions.duration + 'ms)'
118 );
119 }
120
121 if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
122 },
123 testStart: function(name) {
124 tracker.put(name);
125 }
126 });
127};