|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs'); |
| 4 | +var mkdirp = require('mkdirp'); |
| 5 | +var path = require('path'); |
| 6 | +var reporters = require('../../').reporters; |
| 7 | +var XUnit = reporters.XUnit; |
| 8 | + |
| 9 | +describe('XUnit reporter', function () { |
| 10 | + var stdout; |
| 11 | + var stdoutWrite; |
| 12 | + var runner; |
| 13 | + |
| 14 | + beforeEach(function () { |
| 15 | + stdout = []; |
| 16 | + runner = { |
| 17 | + on: function () {} |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + describe('if reporter options output is given', function () { |
| 22 | + var expectedOutput = 'some-output'; |
| 23 | + var options = { |
| 24 | + reporterOptions: { |
| 25 | + output: expectedOutput |
| 26 | + } |
| 27 | + }; |
| 28 | + describe('but it cant create a write stream', function () { |
| 29 | + it('should throw expected error', function () { |
| 30 | + var fsCreateWriteStream = fs.createWriteStream; |
| 31 | + fs.createWriteStream = false; |
| 32 | + |
| 33 | + var boundXUnit = XUnit.bind({}, runner, options); |
| 34 | + boundXUnit.should.throw('file output not supported in browser'); |
| 35 | + fs.createWriteStream = fsCreateWriteStream; |
| 36 | + }); |
| 37 | + }); |
| 38 | + describe('and it can create a write stream', function () { |
| 39 | + it('should locate the output dir, create it, then assign as fileStream', function () { |
| 40 | + var expectedDirectory; |
| 41 | + var mkdirpSync = mkdirp.sync; |
| 42 | + var pathDirname = path.dirname; |
| 43 | + var fsCreateWriteStream = fs.createWriteStream; |
| 44 | + mkdirp.sync = function (directory) { expectedDirectory = directory; }; |
| 45 | + path.dirname = function (location) { return location; }; |
| 46 | + fs.createWriteStream = function (streamDetails) { return streamDetails; }; |
| 47 | + |
| 48 | + var contextVariables = { |
| 49 | + fileStream: null |
| 50 | + }; |
| 51 | + XUnit.call(contextVariables, runner, options); |
| 52 | + |
| 53 | + expectedDirectory.should.equal(expectedOutput); |
| 54 | + contextVariables.fileStream.should.equal(expectedOutput); |
| 55 | + |
| 56 | + fs.createWriteStream = fsCreateWriteStream; |
| 57 | + mkdirp.sync = mkdirpSync; |
| 58 | + path.dirname = pathDirname; |
| 59 | + }); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('on \'pending\', \'pass\' and \'fail\' events', function () { |
| 64 | + it('should add test to tests called on \'end\' event', function () { |
| 65 | + var pendingTest = { |
| 66 | + name: 'pending', |
| 67 | + slow: function () {} |
| 68 | + }; |
| 69 | + var failTest = { |
| 70 | + name: 'fail', |
| 71 | + slow: function () {} |
| 72 | + }; |
| 73 | + var passTest = { |
| 74 | + name: 'pass', |
| 75 | + slow: function () {} |
| 76 | + }; |
| 77 | + runner.on = function (event, callback) { |
| 78 | + if (event === 'pending') { |
| 79 | + callback(pendingTest); |
| 80 | + } |
| 81 | + if (event === 'pass') { |
| 82 | + callback(passTest); |
| 83 | + } |
| 84 | + if (event === 'fail') { |
| 85 | + callback(failTest); |
| 86 | + } |
| 87 | + if (event === 'end') { |
| 88 | + callback(); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + var calledTests = []; |
| 93 | + XUnit.call({ |
| 94 | + write: function () {}, |
| 95 | + test: function (test) { |
| 96 | + calledTests.push(test); |
| 97 | + } |
| 98 | + }, runner); |
| 99 | + |
| 100 | + var expectedCalledTests = [ |
| 101 | + pendingTest, |
| 102 | + passTest, |
| 103 | + failTest |
| 104 | + ]; |
| 105 | + calledTests.should.deepEqual(expectedCalledTests); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('done', function () { |
| 110 | + describe('if fileStream is truthly', function () { |
| 111 | + it('should run callback with failure inside streams end', function () { |
| 112 | + var xunit = new XUnit({on: function () {}}); |
| 113 | + var callbackArgument = null; |
| 114 | + var callback = function (failures) { |
| 115 | + callbackArgument = failures; |
| 116 | + }; |
| 117 | + var calledEnd = false; |
| 118 | + var expectedFailure = 'some-failures'; |
| 119 | + var fileStream = { |
| 120 | + end: function (callback) { |
| 121 | + calledEnd = true; |
| 122 | + callback(); |
| 123 | + } |
| 124 | + }; |
| 125 | + xunit.done.call( |
| 126 | + { fileStream: fileStream }, |
| 127 | + expectedFailure, |
| 128 | + callback |
| 129 | + ); |
| 130 | + |
| 131 | + calledEnd.should.be.true(); |
| 132 | + callbackArgument.should.equal(expectedFailure); |
| 133 | + }); |
| 134 | + }); |
| 135 | + describe('if fileStream is falsy', function () { |
| 136 | + it('should run callback with failure', function () { |
| 137 | + var xunit = new XUnit({on: function () {}}); |
| 138 | + var callbackArgument = null; |
| 139 | + var callback = function (failures) { |
| 140 | + callbackArgument = failures; |
| 141 | + }; |
| 142 | + var expectedFailure = 'some-failures'; |
| 143 | + xunit.done.call( |
| 144 | + { fileStream: false }, |
| 145 | + expectedFailure, |
| 146 | + callback |
| 147 | + ); |
| 148 | + |
| 149 | + callbackArgument.should.equal(expectedFailure); |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('write', function () { |
| 155 | + describe('if fileStream is truthly', function () { |
| 156 | + it('should call fileStream write with line and new line', function () { |
| 157 | + var expectedWrite = null; |
| 158 | + var xunit = new XUnit({on: function () {}}); |
| 159 | + var fileStream = { |
| 160 | + write: function (write) { |
| 161 | + expectedWrite = write; |
| 162 | + } |
| 163 | + }; |
| 164 | + var expectedLine = 'some-line'; |
| 165 | + xunit.write.call( |
| 166 | + { fileStream: fileStream }, |
| 167 | + expectedLine |
| 168 | + ); |
| 169 | + |
| 170 | + expectedWrite.should.equal(expectedLine + '\n'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + describe('if fileStream is falsy and stdout exists', function () { |
| 174 | + it('should call write with line and new line', function () { |
| 175 | + stdoutWrite = process.stdout.write; |
| 176 | + process.stdout.write = function (string) { |
| 177 | + stdout.push(string); |
| 178 | + }; |
| 179 | + |
| 180 | + var xunit = new XUnit({on: function () {}}); |
| 181 | + var expectedLine = 'some-line'; |
| 182 | + xunit.write.call( |
| 183 | + { fileStream: false }, |
| 184 | + expectedLine |
| 185 | + ); |
| 186 | + |
| 187 | + process.stdout.write = stdoutWrite; |
| 188 | + |
| 189 | + stdout[0].should.equal(expectedLine + '\n'); |
| 190 | + }); |
| 191 | + }); |
| 192 | + describe('if fileStream is falsy and stdout does not exist', function () { |
| 193 | + it('should call write with line', function () { |
| 194 | + stdoutWrite = process; |
| 195 | + process = false; // eslint-disable-line no-native-reassign, no-global-assign |
| 196 | + var cachedConsoleLog = console.log; |
| 197 | + console.log = function (string) { |
| 198 | + stdout.push(string); |
| 199 | + }; |
| 200 | + |
| 201 | + var xunit = new XUnit({on: function () {}}); |
| 202 | + var expectedLine = 'some-line'; |
| 203 | + xunit.write.call( |
| 204 | + { fileStream: false }, |
| 205 | + expectedLine |
| 206 | + ); |
| 207 | + |
| 208 | + console.log = cachedConsoleLog; |
| 209 | + process = stdoutWrite; // eslint-disable-line no-native-reassign, no-global-assign |
| 210 | + stdout[0].should.equal(expectedLine); |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | + |
| 215 | + describe('test', function () { |
| 216 | + describe('on test failure', function () { |
| 217 | + it('should write expected tag with error details', function () { |
| 218 | + var xunit = new XUnit({on: function () {}}); |
| 219 | + |
| 220 | + var expectedWrite = null; |
| 221 | + var expectedClassName = 'fullTitle'; |
| 222 | + var expectedTitle = 'some title'; |
| 223 | + var expectedMessage = 'some message'; |
| 224 | + var expectedStack = 'some-stack'; |
| 225 | + var expectedTest = { |
| 226 | + state: 'failed', |
| 227 | + title: expectedTitle, |
| 228 | + parent: { |
| 229 | + fullTitle: function () { |
| 230 | + return expectedClassName; |
| 231 | + } |
| 232 | + }, |
| 233 | + duration: 1000, |
| 234 | + err: { |
| 235 | + message: expectedMessage, |
| 236 | + stack: expectedStack |
| 237 | + } |
| 238 | + }; |
| 239 | + xunit.test.call( |
| 240 | + { |
| 241 | + write: function (string) { |
| 242 | + expectedWrite = string; |
| 243 | + } |
| 244 | + }, |
| 245 | + expectedTest |
| 246 | + ); |
| 247 | + |
| 248 | + var expectedTag = '<testcase classname="' + expectedClassName + '" name="' + expectedTitle + '" time="1"><failure>' + expectedMessage + '\n' + expectedStack + '</failure></testcase>'; |
| 249 | + |
| 250 | + expectedWrite.should.equal(expectedTag); |
| 251 | + }); |
| 252 | + }); |
| 253 | + describe('on test pending', function () { |
| 254 | + it('should write expected tag', function () { |
| 255 | + var xunit = new XUnit({on: function () {}}); |
| 256 | + |
| 257 | + var expectedClassName = 'fullTitle'; |
| 258 | + var expectedTitle = 'some title'; |
| 259 | + var expectedTest = { |
| 260 | + isPending: function () { return true; }, |
| 261 | + title: expectedTitle, |
| 262 | + parent: { |
| 263 | + fullTitle: function () { |
| 264 | + return expectedClassName; |
| 265 | + } |
| 266 | + }, |
| 267 | + duration: 1000 |
| 268 | + }; |
| 269 | + var expectedWrite = null; |
| 270 | + xunit.test.call( |
| 271 | + { |
| 272 | + write: function (string) { |
| 273 | + expectedWrite = string; |
| 274 | + } |
| 275 | + }, |
| 276 | + expectedTest |
| 277 | + ); |
| 278 | + |
| 279 | + var expectedTag = '<testcase classname="' + expectedClassName + '" name="' + expectedTitle + '" time="1"><skipped/></testcase>'; |
| 280 | + |
| 281 | + expectedWrite.should.equal(expectedTag); |
| 282 | + }); |
| 283 | + }); |
| 284 | + describe('on test in any other state', function () { |
| 285 | + it('should write expected tag', function () { |
| 286 | + var xunit = new XUnit({on: function () {}}); |
| 287 | + |
| 288 | + var expectedClassName = 'fullTitle'; |
| 289 | + var expectedTitle = 'some title'; |
| 290 | + var expectedTest = { |
| 291 | + isPending: function () { return false; }, |
| 292 | + title: expectedTitle, |
| 293 | + parent: { |
| 294 | + fullTitle: function () { |
| 295 | + return expectedClassName; |
| 296 | + } |
| 297 | + }, |
| 298 | + duration: false |
| 299 | + }; |
| 300 | + var expectedWrite = null; |
| 301 | + xunit.test.call( |
| 302 | + { |
| 303 | + write: function (string) { |
| 304 | + expectedWrite = string; |
| 305 | + } |
| 306 | + }, |
| 307 | + expectedTest |
| 308 | + ); |
| 309 | + |
| 310 | + var expectedTag = '<testcase classname="' + expectedClassName + '" name="' + expectedTitle + '" time="0"/>'; |
| 311 | + |
| 312 | + expectedWrite.should.equal(expectedTag); |
| 313 | + }); |
| 314 | + }); |
| 315 | + }); |
| 316 | +}); |
0 commit comments