Skip to content

Commit

Permalink
fix: use defineProperty on the error object instead of setting the me…
Browse files Browse the repository at this point in the history
…ssage directly (#1261)

Fixes #1259
  • Loading branch information
julienw authored Sep 17, 2023
1 parent 07f5999 commit 270a531
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,31 @@ test('timeout logs a pretty DOM', async () => {
`)
})

test("timeout doesn't error on DOMExceptions", async () => {
renderIntoDocument(`<div id="pretty">how pretty</div>`)
const error = await waitFor(
() => {
throw new DOMException('nooooooo!')
},
{timeout: 1},
).catch(e => e)
expect(error.message).toMatchInlineSnapshot(`
nooooooo!
Ignored nodes: comments, script, style
<html>
<head />
<body>
<div
id="pretty"
>
how pretty
</div>
</body>
</html>
`)
})

test('should delegate to config.getElementError', async () => {
const elementError = new Error('Custom element error')
const getElementError = jest.fn().mockImplementation(() => elementError)
Expand Down
7 changes: 3 additions & 4 deletions src/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 270a531

Please sign in to comment.