From e6763f90089aae34dbdb840190d4cf27b41df0cc Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Wed, 4 Mar 2020 14:14:40 +0100 Subject: [PATCH] Throw error if an invalid `rootElement` is passed to `assert.dom()` --- lib/qunit-dom.ts | 13 +++++++++++++ tests/acceptance/qunit-dom-test.js | 2 ++ 2 files changed, 15 insertions(+) diff --git a/lib/qunit-dom.ts b/lib/qunit-dom.ts index 58df1460b..f8e909002 100644 --- a/lib/qunit-dom.ts +++ b/lib/qunit-dom.ts @@ -12,6 +12,19 @@ QUnit.assert.dom = function( target?: string | Element | null, rootElement?: Element ): DOMAssertions { + if (!isValidRootElement(rootElement)) { + throw new Error(`${rootElement} is not a valid root element`); + } + rootElement = rootElement || this.dom.rootElement || document; return new DOMAssertions(target || rootElement, rootElement, this); }; + +function isValidRootElement(element: any): element is Element { + return ( + !element || + (typeof element === 'object' && + typeof element.querySelector === 'function' && + typeof element.querySelectorAll === 'function') + ); +} diff --git a/tests/acceptance/qunit-dom-test.js b/tests/acceptance/qunit-dom-test.js index 582dbf39d..20158528f 100644 --- a/tests/acceptance/qunit-dom-test.js +++ b/tests/acceptance/qunit-dom-test.js @@ -39,5 +39,7 @@ module('Acceptance | qunit-dom', function(hooks) { * See this: https://github.com/jsdom/jsdom/issues/1928 */ assert.dom('#with-pseudo-element').hasPseudoElementStyle(':after', { content: '";"' }); + + assert.throws(() => assert.dom('foo', 'bar'), /bar is not a valid root element/); }); });