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