Cleanup toThrowError constructor lookup to work in older IE

- Also use the existing browser detection for phantom
This commit is contained in:
Gregg Van Hove
2017-03-14 14:26:17 -07:00
parent e1f7ca51da
commit 75e652d6a7
4 changed files with 58 additions and 44 deletions
+13 -3
View File
@@ -2933,8 +2933,7 @@ getJasmineRequireObj().toThrowError = function(j$) {
} }
// Get Error constructor of thrown // Get Error constructor of thrown
if (!thrown || !thrown.constructor || !thrown.constructor.constructor || if (!isErrorObject(thrown)) {
!(thrown instanceof (thrown.constructor.constructor('return this')()).Error)) {
fail.message = function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; }; fail.message = function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; };
return fail; return fail;
} }
@@ -3035,7 +3034,18 @@ getJasmineRequireObj().toThrowError = function(j$) {
var Surrogate = function() {}; var Surrogate = function() {};
Surrogate.prototype = type.prototype; Surrogate.prototype = type.prototype;
return (new Surrogate()) instanceof Error; return isErrorObject(new Surrogate());
}
function isErrorObject(thrown) {
if (thrown instanceof Error) {
return true;
}
if (thrown && thrown.constructor && thrown.constructor.constructor &&
(thrown instanceof (thrown.constructor.constructor('return this')()).Error)) {
return true;
}
return false;
} }
} }
+28 -38
View File
@@ -65,51 +65,41 @@ describe("toThrowError", function() {
expect(result.message()).toEqual("Expected function to throw an Error, but it threw 4."); expect(result.message()).toEqual("Expected function to throw an Error, but it threw 4.");
}); });
function isNotRunningInBrowser() { describe("when error is from another frame", function() {
return typeof document === 'undefined' function isNotRunningInBrowser() {
} return typeof document === 'undefined'
function isRunningInPhantomJS(minVersion, maxVersion) {
if (!window.callPhantom) {
return false;
} }
function getVerNum(ver) { var iframe = null;
var nums = ver.split('.'), verNum = 0;
if ((nums[0] = +nums[0])) { verNum += nums[0] * 10000; }
if ((nums[1] = +nums[1])) { verNum += nums[1] * 100; }
if ((nums[2] = +nums[2])) { verNum += nums[2]; }
return verNum;
}
if (minVersion || maxVersion) { afterEach(function() {
var ver = (/\sPhantomJS\/([\d\.]+)\s/.exec(window.navigator.userAgent) || [])[1]; if (iframe !== null) {
if (!ver) { return false; } document.body.removeChild(iframe);
ver = getVerNum(ver); }
if (minVersion && ver < getVerNum(minVersion)) { return false; } });
if (maxVersion && ver > getVerNum(maxVersion)) { return false; }
}
return true;
}
it("passes if thrown is an instanceof Error regardless of global that contains its constructor", function() { it("passes if thrown is an instanceof Error regardless of global that contains its constructor", function() {
if (isNotRunningInBrowser() || isRunningInPhantomJS(null, '1.9.8')) { if (isNotRunningInBrowser() || jasmine.getEnv().phantomVersion < 2 || jasmine.getEnv().ieVersion < 10) {
return; return;
} }
var matcher = jasmineUnderTest.matchers.toThrowError(), var matcher = jasmineUnderTest.matchers.toThrowError();
iframe = document.body.appendChild(document.createElement("iframe")), iframe = document.body.appendChild(document.createElement("iframe"));
iframeDocument = iframe.contentDocument, iframe.src = "about:blank";
result; var iframeDocument = iframe.contentWindow.document;
iframeDocument.body.appendChild(iframeDocument.createElement("script")) if (iframeDocument.body) {
.textContent = "function method() { throw new Error('foo'); }"; iframeDocument.body.appendChild(iframeDocument.createElement("script"))
.textContent = "function method() { throw new Error('foo'); }";
} else {
// older IE
iframeDocument.write("<html><head><script>function method() { throw new Error('foo'); }</script></head></html>");
}
result = matcher.compare(iframe.contentWindow.method); var result = matcher.compare(iframe.contentWindow.method);
expect(result.pass).toBe(true); expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an Error, but it threw Error."); expect(result.message).toEqual("Expected function not to throw an Error, but it threw Error.");
});
document.body.removeChild(iframe);
}); });
it("fails with the correct message if thrown is a falsy value", function() { it("fails with the correct message if thrown is a falsy value", function() {
+4
View File
@@ -20,4 +20,8 @@
return /Firefox\/([0-9]{0,})/.exec(userAgent); return /Firefox\/([0-9]{0,})/.exec(userAgent);
}); });
env.phantomVersion = browserVersion(function(userAgent) {
return /PhantomJs\/([0-9.]{0,})+/.exec(userAgent);
});
})(jasmine.getEnv()); })(jasmine.getEnv());
+13 -3
View File
@@ -29,8 +29,7 @@ getJasmineRequireObj().toThrowError = function(j$) {
} }
// Get Error constructor of thrown // Get Error constructor of thrown
if (!thrown || !thrown.constructor || !thrown.constructor.constructor || if (!isErrorObject(thrown)) {
!(thrown instanceof (thrown.constructor.constructor('return this')()).Error)) {
fail.message = function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; }; fail.message = function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; };
return fail; return fail;
} }
@@ -131,7 +130,18 @@ getJasmineRequireObj().toThrowError = function(j$) {
var Surrogate = function() {}; var Surrogate = function() {};
Surrogate.prototype = type.prototype; Surrogate.prototype = type.prototype;
return (new Surrogate()) instanceof Error; return isErrorObject(new Surrogate());
}
function isErrorObject(thrown) {
if (thrown instanceof Error) {
return true;
}
if (thrown && thrown.constructor && thrown.constructor.constructor &&
(thrown instanceof (thrown.constructor.constructor('return this')()).Error)) {
return true;
}
return false;
} }
} }