From ed232548650c6ccfc24e56c9439322556753753c Mon Sep 17 00:00:00 2001 From: anseki Date: Fri, 10 Feb 2017 17:17:39 +0900 Subject: [PATCH 1/2] fix #1252 --- spec/core/matchers/toThrowErrorSpec.js | 24 ++++++++++++++++++++++++ src/core/matchers/toThrowError.js | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/spec/core/matchers/toThrowErrorSpec.js b/spec/core/matchers/toThrowErrorSpec.js index c15e7129..bdba3edc 100644 --- a/spec/core/matchers/toThrowErrorSpec.js +++ b/spec/core/matchers/toThrowErrorSpec.js @@ -65,6 +65,30 @@ describe("toThrowError", function() { expect(result.message()).toEqual("Expected function to throw an Error, but it threw 4."); }); + function isNotRunningInBrowser() { + return typeof document === 'undefined' + } + + it("passes if thrown is an instanceof Error regardless of global that contains its constructor", function() { + if (isNotRunningInBrowser()) { + return; + } + + var matcher = jasmineUnderTest.matchers.toThrowError(), + iframe = document.body.appendChild(document.createElement("iframe")), + iframeDocument = iframe.contentDocument, + result; + + iframeDocument.body.appendChild(iframeDocument.createElement("script")) + .textContent = "function method() { throw new Error('foo'); }"; + + result = matcher.compare(iframe.contentWindow.method); + expect(result.pass).toBe(true); + 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() { var matcher = jasmineUnderTest.matchers.toThrowError(), fn = function() { diff --git a/src/core/matchers/toThrowError.js b/src/core/matchers/toThrowError.js index 67a880f9..f6859c61 100644 --- a/src/core/matchers/toThrowError.js +++ b/src/core/matchers/toThrowError.js @@ -28,7 +28,9 @@ getJasmineRequireObj().toThrowError = function(j$) { return fail; } - if (!(thrown instanceof Error)) { + // Get Error constructor of thrown + if (!thrown || !thrown.constructor || !thrown.constructor.constructor || + !(thrown instanceof (thrown.constructor.constructor('return this')()).Error)) { fail.message = function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; }; return fail; } From 19b89d437e90e7212267ee9b543d35306eae3935 Mon Sep 17 00:00:00 2001 From: anseki Date: Thu, 16 Feb 2017 14:50:24 +0900 Subject: [PATCH 2/2] Skip a spec in PhantomJS that has bugs about iframe --- spec/core/matchers/toThrowErrorSpec.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/spec/core/matchers/toThrowErrorSpec.js b/spec/core/matchers/toThrowErrorSpec.js index bdba3edc..db7050c7 100644 --- a/spec/core/matchers/toThrowErrorSpec.js +++ b/spec/core/matchers/toThrowErrorSpec.js @@ -69,8 +69,31 @@ describe("toThrowError", function() { return typeof document === 'undefined' } + function isRunningInPhantomJS(minVersion, maxVersion) { + if (!window.callPhantom) { + return false; + } + + function getVerNum(ver) { + 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) { + var ver = (/\sPhantomJS\/([\d\.]+)\s/.exec(window.navigator.userAgent) || [])[1]; + if (!ver) { return false; } + 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() { - if (isNotRunningInBrowser()) { + if (isNotRunningInBrowser() || isRunningInPhantomJS(null, '1.9.8')) { return; }