1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var path = require('path');
|
11 | var nodeunit = require('nodeunit');
|
12 | var nodeunitUtils = require('nodeunit/lib/utils');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var currentModule;
|
20 |
|
21 | var unfinished = {};
|
22 |
|
23 |
|
24 | process.on('exit', function() {
|
25 | var len = Object.keys(unfinished).length;
|
26 |
|
27 | if (len > 0) {
|
28 | log.muted = false;
|
29 | verbose.error().or.writeln('F'.red);
|
30 | log.error('Incomplete tests/setups/teardowns:');
|
31 | Object.keys(unfinished).forEach(log.error, log);
|
32 | fail.fatal('A test was missing test.done(), so nodeunit exploded. Sorry!',
|
33 | Math.min(99, 90 + len));
|
34 | }
|
35 | });
|
36 |
|
37 |
|
38 | var failedAssertions = [];
|
39 | function logFailedAssertions() {
|
40 | var assertion, stack;
|
41 |
|
42 | while (assertion = failedAssertions.shift()) {
|
43 | nodeunitUtils.betterErrors(assertion);
|
44 | verbose.or.error(assertion.testName);
|
45 | if (assertion.error.name === 'AssertionError' && assertion.message) {
|
46 | log.error('AssertionMessage: ' + assertion.message.magenta);
|
47 | }
|
48 | stack = assertion.error.stack.replace(/ {4}(at)/g, ' $1');
|
49 | stack = stack.replace(/:(.*?\n)/, '$1'.magenta);
|
50 | log.error(stack + '\n').writeln();
|
51 | }
|
52 | }
|
53 |
|
54 |
|
55 | nodeunit.reporters.grunt = {
|
56 | info: 'Grunt reporter',
|
57 | run: function(files, options, callback) {
|
58 | var opts = {
|
59 |
|
60 | testspec: undefined,
|
61 |
|
62 |
|
63 | moduleStart: function(name) {
|
64 |
|
65 |
|
66 | currentModule = name;
|
67 | verbose.subhead('Testing ' + name).or.write('Testing ' + name);
|
68 | },
|
69 |
|
70 |
|
71 | moduleDone: function(name) {
|
72 |
|
73 | if (name !== currentModule) { return; }
|
74 |
|
75 | if (!option('verbose')) {
|
76 | if (failedAssertions.length > 0) {
|
77 | log.writeln();
|
78 | logFailedAssertions();
|
79 | } else {
|
80 | log.ok();
|
81 | }
|
82 | }
|
83 | },
|
84 |
|
85 | testStart: function(name) {
|
86 |
|
87 |
|
88 | unfinished[name] = name;
|
89 | verbose.write(name + '...');
|
90 |
|
91 |
|
92 | log.muted = true;
|
93 | },
|
94 |
|
95 | testDone: function(name, assertions) {
|
96 | delete unfinished[name];
|
97 |
|
98 | log.muted = false;
|
99 |
|
100 | if (assertions.failures()) {
|
101 | assertions.forEach(function(ass) {
|
102 | if (ass.failed()) {
|
103 | ass.testName = name;
|
104 | failedAssertions.push(ass);
|
105 | }
|
106 | });
|
107 | if (option('verbose')) {
|
108 | log.error();
|
109 | logFailedAssertions();
|
110 | } else {
|
111 | log.write('F'.red);
|
112 | }
|
113 | } else {
|
114 | verbose.ok().or.write('.');
|
115 | }
|
116 | },
|
117 |
|
118 | done: function (assertions) {
|
119 | if (assertions.failures()) {
|
120 | fail.warn(assertions.failures() + '/' + assertions.length +
|
121 | ' assertions failed (' + assertions.duration + 'ms)',
|
122 | Math.min(99, 90 + assertions.failures()));
|
123 | } else {
|
124 | verbose.writeln();
|
125 | log.ok(assertions.length + ' assertions passed (' +
|
126 | assertions.duration + 'ms)');
|
127 | }
|
128 |
|
129 | callback();
|
130 | }
|
131 | };
|
132 |
|
133 |
|
134 | var paths = files.map(function (filepath) {
|
135 | return path.join(process.cwd(), filepath);
|
136 | });
|
137 | nodeunit.runFiles(paths, opts);
|
138 | }
|
139 | };
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | task.registerBasicTask('test', 'Run unit tests.', function(data, name) {
|
146 |
|
147 | var filepaths = file.expand(data);
|
148 |
|
149 |
|
150 | file.clearRequireCache(filepaths);
|
151 |
|
152 | nodeunit.reporters.grunt.run(filepaths, {}, this.async());
|
153 | });
|