jcayzac
jcayzac12mo ago

assertThrows() limitations

assertThrows() seems to only filter on class name and message. Is there an assert function for checking whether code throws a DOMException with a specific name?
3 Replies
jcayzac
jcayzacOP12mo ago
Ah I guess I can simply do:
const { name } = assertThrows(() => { … }, DOMException))
assertEquals(name, 'AbortError')
const { name } = assertThrows(() => { … }, DOMException))
assertEquals(name, 'AbortError')
I had missed that form of assertThrows() returns the error. Now, it's an async call I have in my try/catch, and assertThrows() doesn't support PromiseLike… I just want a std assert I can replace the following with:
try {
await
} catch (ex) {
assertInstanceOf(ex, DOMException)
assertEquals(ex.name, 'AbortError')
}
try {
await
} catch (ex) {
assertInstanceOf(ex, DOMException)
assertEquals(ex.name, 'AbortError')
}
I wish I could do
const { name } = await assertThrows(async () => { … }, DOMException))
assertEquals(name, 'AbortError')
const { name } = await assertThrows(async () => { … }, DOMException))
assertEquals(name, 'AbortError')
jcayzac
jcayzacOP12mo ago
GitHub
[assert] support async code in assertThrows() · Issue #3999 · denol...
For synchronous code, it is possible to replace: try { doSomething() } catch (ex) { assertInstanceOf(ex, DOMException) assertEquals(ex.name, 'AbortError') } with just: const { name } = asse...
Deno AI Helper
Deno AI Helper12mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on your message, it seems like you're looking for a way to assert that a specific type of DOMException is thrown with a particular name. While I can't provide a direct answer from the documentation, I can suggest that if assertThrows() does not meet your requirements, you might consider writing a custom assertion function to check for the specific DOMException and its name. This could involve try-catch blocks to capture the exception and then assertions to check the name property of the caught exception. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this advice. If you need further assistance, you might want to explore community resources or the Deno manual for more information on error handling and custom assertions.