karma-jasmine
Advanced tools
Comparing version 0.3.3 to 0.3.4
(function(window) { | ||
var formatFailedStep = function(step) { | ||
var stack = step.stack; | ||
var message = step.message; | ||
/** | ||
* Decision maker for whether a stack entry is considered relevant. | ||
* @param {String} entry Error stack entry. | ||
* @return {Boolean} True if relevant, False otherwise. | ||
*/ | ||
function isRelevantStackEntry(entry) { | ||
// discard empty and falsy entries: | ||
return (entry ? true : false) && | ||
// discard entries related to jasmine and karma-jasmine: | ||
!/\/(jasmine-core|karma-jasmine)\//.test(entry) && | ||
// discard karma specifics, e.g. "at http://localhost:7018/karma.js:185" | ||
!/\/(karma.js|context.html):/.test(entry); | ||
} | ||
if (stack) { | ||
// remove the trailing dot | ||
var firstLine = stack.substring(0, stack.indexOf('\n') - 1); | ||
/** | ||
* Returns relevant stack entries. | ||
* @param {String} stack Complete error stack trace. | ||
* @return {Array} A list of relevant stack entries. | ||
*/ | ||
function getRelevantStackFrom(stack) { | ||
var relevantStack = []; | ||
if (message && message.indexOf(firstLine) === -1) { | ||
stack = message +'\n'+ stack; | ||
stack = stack.split('\n'); | ||
for (var i = 0; i < stack.length; i += 1) { | ||
if (isRelevantStackEntry(stack[i])) { | ||
relevantStack.push(stack[i]); | ||
} | ||
} | ||
// remove jasmine stack entries | ||
return stack.replace(/\n.+jasmine\.js\?\w*\:.+(?=(\n|$))/g, ''); | ||
return relevantStack; | ||
} | ||
/** | ||
* Custom formatter for a failed step. | ||
* | ||
* Different browsers report stack trace in different ways. This function | ||
* attempts to provide a concise, relevant error message by removing the | ||
* unnecessary stack traces coming from the testing framework itself as well | ||
* as possible repetition. | ||
* | ||
* @see https://github.com/karma-runner/karma-jasmine/issues/60 | ||
* @param {Object} step Step object with stack and message properties. | ||
* @return {String} Formatted step. | ||
*/ | ||
function formatFailedStep(step) { | ||
// Safari seems to have no stack trace, | ||
// so we just return the error message: | ||
if (!step.stack) { return step.message; } | ||
var relevantMessage = []; | ||
var relevantStack = []; | ||
var dirtyRelevantStack = getRelevantStackFrom(step.stack); | ||
// PhantomJS returns multiline error message for errors coming from specs | ||
// (for example when calling a non-existing function). This error is present | ||
// in both `step.message` and `step.stack` at the same time, but stack seems | ||
// preferable, so we iterate relevant stack, compare it to message: | ||
for (var i = 0; i < dirtyRelevantStack.length; i += 1) { | ||
if (step.message && step.message.indexOf(dirtyRelevantStack[i]) === -1) { | ||
// Stack entry is not in the message, | ||
// we consider it to be a relevant stack: | ||
relevantStack.push(dirtyRelevantStack[i]); | ||
} else { | ||
// Stack entry is already in the message, | ||
// we consider it to be a suitable message alternative: | ||
relevantMessage.push(dirtyRelevantStack[i]); | ||
} | ||
} | ||
return message; | ||
}; | ||
// In most cases the above will leave us with an empty message... | ||
if (relevantMessage.length === 0) { | ||
// Let's reuse the original message: | ||
relevantMessage.push(step.message); | ||
// Now we probably have a repetition case where: | ||
// relevantMessage: ["Expected true to be false."] | ||
// relevantStack: ["Error: Expected true to be false.", ...] | ||
if (relevantStack[0].indexOf(step.message) !== -1) { | ||
// The message seems preferable, so we remove the first value from | ||
// the stack to get rid of repetition : | ||
relevantStack.shift(); | ||
} | ||
} | ||
// Example output: | ||
// -------------------- | ||
// Chrome 40.0.2214 (Mac OS X 10.9.5) xxx should return false 1 FAILED | ||
// Expected true to be false | ||
// at /foo/bar/baz.spec.js:22:13 | ||
// at /foo/bar/baz.js:18:29 | ||
return relevantMessage.concat(relevantStack).join('\n'); | ||
} | ||
var indexOf = function(collection, item) { | ||
@@ -24,0 +99,0 @@ if (collection.indexOf) { |
{ | ||
"name": "karma-jasmine", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "A Karma plugin - adapter for Jasmine testing framework.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13307
265