From 0575f8e871b7d85a3c2751e99789b738d6313bfe Mon Sep 17 00:00:00 2001 From: Jaga Santagostino Date: Sat, 5 Oct 2019 12:40:14 +0200 Subject: [PATCH] improve error messages for .toBeInTheDocument --- src/to-be-in-the-document.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/to-be-in-the-document.js b/src/to-be-in-the-document.js index 9b8fa696..c0a05331 100644 --- a/src/to-be-in-the-document.js +++ b/src/to-be-in-the-document.js @@ -13,6 +13,15 @@ export function toBeInTheDocument(element) { const pass = element === null ? false : element.ownerDocument.contains(element) + const errorFound = () => { + return `expected document not to contain element, found ${stringify( + element.cloneNode(true), + )} instead` + } + const errorNotFound = () => { + return `element could not be found in the document` + } + return { pass, message: () => { @@ -23,10 +32,7 @@ export function toBeInTheDocument(element) { '', ), '', - receivedColor(`${stringify(element.ownerDocument.cloneNode(false))} ${ - this.isNot ? 'contains:' : 'does not contain:' - } ${stringify(element.cloneNode(false))} - `), + receivedColor(this.isNot ? errorFound() : errorNotFound()), ].join('\n') }, }