From 270a531bcd8bee2ccaaa96a477619f14422faebe Mon Sep 17 00:00:00 2001
From: Julien Wajsberg
Date: Sun, 17 Sep 2023 20:07:57 +0200
Subject: [PATCH] fix: use defineProperty on the error object instead of
setting the message directly (#1261)
Fixes #1259
---
src/__tests__/wait-for.js | 25 +++++++++++++++++++++++++
src/wait-for.js | 7 +++----
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/__tests__/wait-for.js b/src/__tests__/wait-for.js
index 03814020..d956525d 100644
--- a/src/__tests__/wait-for.js
+++ b/src/__tests__/wait-for.js
@@ -148,6 +148,31 @@ test('timeout logs a pretty DOM', async () => {
`)
})
+test("timeout doesn't error on DOMExceptions", async () => {
+ renderIntoDocument(`how pretty
`)
+ const error = await waitFor(
+ () => {
+ throw new DOMException('nooooooo!')
+ },
+ {timeout: 1},
+ ).catch(e => e)
+ expect(error.message).toMatchInlineSnapshot(`
+ nooooooo!
+
+ Ignored nodes: comments, script, style
+
+
+
+
+ how pretty
+
+
+
+ `)
+})
+
test('should delegate to config.getElementError', async () => {
const elementError = new Error('Custom element error')
const getElementError = jest.fn().mockImplementation(() => elementError)
diff --git a/src/wait-for.js b/src/wait-for.js
index 79c53a38..ab2c9989 100644
--- a/src/wait-for.js
+++ b/src/wait-for.js
@@ -24,10 +24,9 @@ function waitFor(
stackTraceError,
interval = 50,
onTimeout = error => {
- error.message = getConfig().getElementError(
- error.message,
- container,
- ).message
+ Object.defineProperty(error, 'message', {
+ value: getConfig().getElementError(error.message, container).message,
+ })
return error
},
mutationObserverOptions = {