From dc80a282baf961fce8720ab87529630568da3496 Mon Sep 17 00:00:00 2001 From: Bjarki Date: Thu, 1 Jul 2021 20:21:44 +0000 Subject: [PATCH] Make j$.isError_ compatible with Trusted Types The isError_ check uses a heuristic that calls the Function constructor to determine if the given value is an Error object. This results in a runtime violation in test suites that enforce Trusted Types. Since Trusted Types are only supported in modern browsers (currently, Chromium-based browsers), we can use a more straightforward heuristic in environments where Trusted Types are supported. Fixes #1910. See that thread for more details. --- lib/jasmine-core/jasmine.js | 10 ++++++++++ src/core/base.js | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 1ad0935a..406c6b67 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -270,6 +270,16 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { if (value instanceof Error) { return true; } + if ( + typeof window !== 'undefined' && + typeof window.trustedTypes !== 'undefined' + ) { + return ( + value && + typeof value.stack === 'string' && + typeof value.message === 'string' + ); + } if (value && value.constructor && value.constructor.constructor) { var valueGlobal = value.constructor.constructor('return this'); if (j$.isFunction_(valueGlobal)) { diff --git a/src/core/base.js b/src/core/base.js index c40c6559..1d31d89d 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -102,6 +102,16 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { if (value instanceof Error) { return true; } + if ( + typeof window !== 'undefined' && + typeof window.trustedTypes !== 'undefined' + ) { + return ( + value && + typeof value.stack === 'string' && + typeof value.message === 'string' + ); + } if (value && value.constructor && value.constructor.constructor) { var valueGlobal = value.constructor.constructor('return this'); if (j$.isFunction_(valueGlobal)) {