diff --git a/lib/empower.js b/lib/empower.js index c1b0002..5428629 100644 --- a/lib/empower.js +++ b/lib/empower.js @@ -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 { @@ -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); @@ -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) : {},