UNPKG

4.12 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 = "Default 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 tracker = track.createTracker(function (tracker) {
59 if (tracker.unfinished()) {
60 console.log('');
61 console.log(error(bold(
62 'FAILURES: Undone tests (or their setups/teardowns): '
63 )));
64 var names = tracker.names();
65 for (var i = 0; i < names.length; i += 1) {
66 console.log('- ' + names[i]);
67 }
68 console.log('');
69 console.log('To fix this, make sure all tests call test.done()');
70 process.reallyExit(tracker.unfinished());
71 }
72 });
73
74 var opts = {
75 testspec: options.testspec,
76 testFullSpec: options.testFullSpec,
77 recursive: options.recursive,
78 moduleStart: function (name) {
79 console.log('\n' + bold(name));
80 },
81 testDone: function (name, assertions) {
82 tracker.remove(name);
83
84 if (!assertions.failures()) {
85 console.log(pass_indicator + ' ' + name);
86 }
87 else {
88 console.log(error(fail_indicator + ' ' + name) + '\n');
89 assertions.forEach(function (a) {
90 if (a.failed()) {
91 a = utils.betterErrors(a);
92 if (a.error instanceof AssertionError && a.message) {
93 console.log(
94 'Assertion Message: ' +
95 assertion_message(a.message)
96 );
97 }
98 console.log(a.error.stack + '\n');
99 }
100 });
101 }
102 },
103 done: function (assertions, end) {
104 var end = end || new Date().getTime();
105 var duration = end - start;
106 if (assertions.failures()) {
107 console.log(
108 '\n' + bold(error('FAILURES: ')) + assertions.failures() +
109 '/' + assertions.length + ' assertions failed (' +
110 assertions.duration + 'ms)'
111 );
112 }
113 else {
114 console.log(
115 '\n' + bold(ok('OK: ')) + assertions.length +
116 ' assertions (' + assertions.duration + 'ms)'
117 );
118 }
119
120 if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
121 },
122 testStart: function(name) {
123 tracker.put(name);
124 }
125 };
126 if (files && files.length) {
127 var paths = files.map(function (p) {
128 return path.resolve(p);
129 });
130 nodeunit.runFiles(paths, opts);
131 } else {
132 nodeunit.runModules(files,opts);
133 }
134};