| 1 |
"use strict"; |
| 2 |
|
| 3 |
var sinon = require("sinon"); |
| 4 |
var errors = require("../../lib/errors"); |
| 5 |
var createStatsCollector = require("../../lib/stats-collector"); |
| 6 |
|
| 7 |
var createUnsupportedError = errors.createUnsupportedError; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
function createMockRunner(runStr, ifStr1, ifStr2, ifStr3, arg1, arg2) { |
| 21 |
var runnerFunction = createRunnerFunction( |
| 22 |
runStr, |
| 23 |
ifStr1, |
| 24 |
ifStr2, |
| 25 |
ifStr3, |
| 26 |
arg1, |
| 27 |
arg2, |
| 28 |
); |
| 29 |
var mockRunner = { |
| 30 |
on: runnerFunction, |
| 31 |
once: runnerFunction, |
| 32 |
}; |
| 33 |
createStatsCollector(mockRunner); |
| 34 |
return mockRunner; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
function createRunnerFunction(runStr, ifStr1, ifStr2, ifStr3, arg1, arg2) { |
| 52 |
var test = null; |
| 53 |
switch (runStr) { |
| 54 |
case "start": |
| 55 |
case "pending": |
| 56 |
case "end": |
| 57 |
return function (event, callback) { |
| 58 |
if (event === ifStr1) { |
| 59 |
callback(); |
| 60 |
} |
| 61 |
}; |
| 62 |
case "pending test": |
| 63 |
case "pass": |
| 64 |
case "fail": |
| 65 |
case "suite": |
| 66 |
case "suite end": |
| 67 |
case "test end": |
| 68 |
test = arg1; |
| 69 |
return function (event, callback) { |
| 70 |
if (event === ifStr1) { |
| 71 |
callback(test); |
| 72 |
} |
| 73 |
}; |
| 74 |
case "fail two args": |
| 75 |
test = arg1; |
| 76 |
var expectedError = arg2; |
| 77 |
return function (event, callback) { |
| 78 |
if (event === ifStr1) { |
| 79 |
callback(test, expectedError); |
| 80 |
} |
| 81 |
}; |
| 82 |
case "start test": |
| 83 |
test = arg1; |
| 84 |
return function (event, callback) { |
| 85 |
if (event === ifStr1) { |
| 86 |
callback(); |
| 87 |
} |
| 88 |
if (event === ifStr2) { |
| 89 |
callback(test); |
| 90 |
} |
| 91 |
}; |
| 92 |
case "suite suite end": |
| 93 |
var expectedSuite = arg1; |
| 94 |
return function (event, callback) { |
| 95 |
if (event === ifStr1) { |
| 96 |
callback(expectedSuite); |
| 97 |
} |
| 98 |
if (event === ifStr2) { |
| 99 |
callback(); |
| 100 |
} |
| 101 |
if (event === ifStr3) { |
| 102 |
callback(); |
| 103 |
} |
| 104 |
}; |
| 105 |
case "pass end": |
| 106 |
test = arg1; |
| 107 |
return function (event, callback) { |
| 108 |
if (event === ifStr1) { |
| 109 |
callback(test); |
| 110 |
} |
| 111 |
if (event === ifStr2) { |
| 112 |
callback(); |
| 113 |
} |
| 114 |
}; |
| 115 |
case "test end fail": |
| 116 |
test = arg1; |
| 117 |
var error = arg2; |
| 118 |
return function (event, callback) { |
| 119 |
if (event === ifStr1) { |
| 120 |
callback(); |
| 121 |
} |
| 122 |
if (event === ifStr2) { |
| 123 |
callback(test, error); |
| 124 |
} |
| 125 |
}; |
| 126 |
case "fail end pass": |
| 127 |
return function (event, callback) { |
| 128 |
test = arg1; |
| 129 |
if (event === ifStr1) { |
| 130 |
callback(test, {}); |
| 131 |
} |
| 132 |
if (event === ifStr2) { |
| 133 |
callback(test); |
| 134 |
} |
| 135 |
if (event === ifStr3) { |
| 136 |
callback(test); |
| 137 |
} |
| 138 |
}; |
| 139 |
default: |
| 140 |
throw createUnsupportedError( |
| 141 |
"This function does not support the runner string specified.", |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
function makeTest(err) { |
| 147 |
return { |
| 148 |
err, |
| 149 |
titlePath: function () { |
| 150 |
return ["test title"]; |
| 151 |
}, |
| 152 |
}; |
| 153 |
} |
| 154 |
|
| 155 |
function createElements(argObj) { |
| 156 |
var res = []; |
| 157 |
for (var i = argObj.from; i <= argObj.to; i++) { |
| 158 |
res.push("element " + i); |
| 159 |
} |
| 160 |
return res; |
| 161 |
} |
| 162 |
|
| 163 |
function makeExpectedTest( |
| 164 |
expectedTitle, |
| 165 |
expectedFullTitle, |
| 166 |
expectedFile, |
| 167 |
expectedDuration, |
| 168 |
currentRetry, |
| 169 |
) { |
| 170 |
return { |
| 171 |
title: expectedTitle, |
| 172 |
fullTitle: function () { |
| 173 |
return expectedFullTitle; |
| 174 |
}, |
| 175 |
file: expectedFile, |
| 176 |
duration: expectedDuration, |
| 177 |
currentRetry: function () { |
| 178 |
return currentRetry; |
| 179 |
}, |
| 180 |
slow: function () {}, |
| 181 |
}; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
function createRunReporterFunction(ctor) { |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
var runReporter = function (stubSelf, runner, options, tee) { |
| 201 |
var origStdoutWrite = process.stdout.write; |
| 202 |
var stdoutWriteStub = sinon.stub(process.stdout, "write"); |
| 203 |
var stdout = []; |
| 204 |
|
| 205 |
var gather = function (chunk) { |
| 206 |
stdout.push(chunk); |
| 207 |
if (tee) { |
| 208 |
origStdoutWrite.call(process.stdout, chunk); |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
stdoutWriteStub.callsFake(gather); |
| 214 |
|
| 215 |
|
| 216 |
Object.setPrototypeOf(stubSelf, ctor.prototype); |
| 217 |
|
| 218 |
try { |
| 219 |
|
| 220 |
ctor.call(stubSelf, runner, options); |
| 221 |
} finally { |
| 222 |
|
| 223 |
|
| 224 |
stdoutWriteStub.restore(); |
| 225 |
} |
| 226 |
|
| 227 |
return stdout; |
| 228 |
}; |
| 229 |
|
| 230 |
return runReporter; |
| 231 |
} |
| 232 |
|
| 233 |
module.exports = { |
| 234 |
createElements, |
| 235 |
createMockRunner, |
| 236 |
createRunReporterFunction, |
| 237 |
makeExpectedTest, |
| 238 |
makeTest, |
| 239 |
}; |