Skip to content

Commit

Permalink
Add PhantomJS workaround.
Browse files Browse the repository at this point in the history
Modified message and context in catch block will be lost when rethrowing errors on PhantomJS ?
  • Loading branch information
twada committed Mar 30, 2014
1 parent 34420c1 commit 047e920
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions lib/empower.js
Expand Up @@ -14,18 +14,23 @@
*/
(function (root, factory) {
'use strict';
var isPhantom = (typeof root.callPhantom === 'function');

// using returnExports UMD pattern
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
module.exports = factory(isPhantom);
} else {
root.empower = factory();
root.empower = factory(isPhantom);
}
}(this, function () {
}(this, function (isPhantom) {
'use strict';

// How to pass isPhantom to AMD define function in UMD style?
if (typeof define === 'function' && define.amd) {
isPhantom = (typeof window.callPhantom === 'function');
}

function defaultOptions () {
return {
Expand Down Expand Up @@ -105,6 +110,7 @@
function enhance (target, formatter, config) {
var eagerEvaluation = !(config.modifyMessageOnFail || config.saveContextOnFail),
doPowerAssert = function (baseAssert, args, message, context) {
var f;
if (eagerEvaluation) {
args.push(buildPowerAssertText(message, context));
return baseAssert.apply(target, args);
Expand All @@ -116,16 +122,29 @@
if (e.name !== 'AssertionError') {
throw e;
}
if (typeof target.AssertionError !== 'function') {
throw e;
}
if (isPhantom) {
f = new target.AssertionError({
actual: e.actual,
expected: e.expected,
operator: e.operator,
message: e.message
});
} else {
f = e;
}
if (config.modifyMessageOnFail) {
e.message = buildPowerAssertText(message, context);
f.message = buildPowerAssertText(message, context);
if (typeof e.generatedMessage !== 'undefined') {
e.generatedMessage = false;
f.generatedMessage = false;
}
}
if (config.saveContextOnFail) {
e.powerAssertContext = context;
f.powerAssertContext = context;
}
throw e;
throw f;
}
},
enhancement = (typeof target === 'function') ? decorateOneArg(target, target, doPowerAssert) : {},
Expand Down

0 comments on commit 047e920

Please sign in to comment.