UNPKG

4.17 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 path = require('path'),
15 track = require('../track'),
16 AssertionError = require('../assert').AssertionError;
17
18/**
19 * Reporter info string
20 */
21
22exports.info = "Pretty minimal output";
23
24/**
25 * Run all tests within each module, reporting the results to the command-line.
26 *
27 * @param {Array} files
28 * @api public
29 */
30
31exports.run = function (files, options, callback) {
32
33 if (!options) {
34 // load default options
35 var content = fs.readFileSync(
36 __dirname + '/../../bin/nodeunit.json', 'utf8'
37 );
38 options = JSON.parse(content);
39 }
40
41 var red = function (str) {
42 return options.error_prefix + str + options.error_suffix;
43 };
44 var green = function (str) {
45 return options.ok_prefix + str + options.ok_suffix;
46 };
47 var magenta = function (str) {
48 return options.assertion_prefix + str + options.assertion_suffix;
49 };
50 var bold = function (str) {
51 return options.bold_prefix + str + options.bold_suffix;
52 };
53
54 var start = new Date().getTime();
55
56 var tracker = track.createTracker(function (tracker) {
57 if (tracker.unfinished()) {
58 console.log('');
59 console.log(bold(red(
60 'FAILURES: Undone tests (or their setups/teardowns): '
61 )));
62 var names = tracker.names();
63 for (var i = 0; i < names.length; i += 1) {
64 console.log('- ' + names[i]);
65 }
66 console.log('');
67 console.log('To fix this, make sure all tests call test.done()');
68 process.reallyExit(tracker.unfinished());
69 }
70 });
71
72
73 var opts = {
74 testspec: options.testspec,
75 testFullSpec: options.testFullSpec,
76 moduleStart: function (name) {
77 process.stdout.write(bold(name) + ': ');
78 },
79 moduleDone: function (name, assertions) {
80 console.log('');
81 if (assertions.failures()) {
82 assertions.forEach(function (a) {
83 if (a.failed()) {
84 a = utils.betterErrors(a);
85 if (a.error instanceof AssertionError && a.message) {
86 console.log(
87 'Assertion in test ' + bold(a.testname) + ': ' +
88 magenta(a.message)
89 );
90 }
91 console.log(a.error.stack + '\n');
92 }
93 });
94 }
95
96 },
97 testStart: function (name) {
98 tracker.put(name);
99 },
100 testDone: function (name, assertions) {
101 tracker.remove(name);
102
103 if (!assertions.failures()) {
104 process.stdout.write('.');
105 }
106 else {
107 process.stdout.write(red('F'));
108 assertions.forEach(function (assertion) {
109 assertion.testname = name;
110 });
111 }
112 },
113 done: function (assertions) {
114 var end = new Date().getTime();
115 var duration = end - start;
116 if (assertions.failures()) {
117 console.log(
118 '\n' + bold(red('FAILURES: ')) + assertions.failures() +
119 '/' + assertions.length + ' assertions failed (' +
120 assertions.duration + 'ms)'
121 );
122 }
123 else {
124 console.log(
125 '\n' + bold(green('OK: ')) + assertions.length +
126 ' assertions (' + assertions.duration + 'ms)'
127 );
128 }
129
130 if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
131 }
132 };
133
134 if (files && files.length) {
135 var paths = files.map(function (p) {
136 return path.resolve(p);
137 });
138 nodeunit.runFiles(paths, opts);
139 } else {
140 nodeunit.runModules(files,opts);
141 }
142};