Skip to content

Commit

Permalink
Merge pull request #14 from mbland/improve-error-rethrows
Browse files Browse the repository at this point in the history
Rethrow Jsdom import errors properly
  • Loading branch information
mbland authored Jan 5, 2024
2 parents cd9a3f3 + fc06c9a commit de8bc48
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class JsdomPageOpener {
try {
await this.#importModules(window, document)
} catch (err) {
throw new Error(`error importing modules from ${pagePath}: ${err}`)
throw new Error(`opening ${pagePath}`, { cause: err })
}
return { window, document, close() { window.close() } }
}
Expand Down Expand Up @@ -159,6 +159,6 @@ function importModules(doc) {
const modules = Array.from(doc.querySelectorAll('script[type="module"]'))
return Promise.all(modules.filter(m => m.src).map(async m => {
try { await import(m.src) }
catch (err) { throw new Error(`error importing ${m.src}: ${err}`) }
catch (err) { throw new Error(`importing ${m.src}`, { cause: err }) }
}))
}
10 changes: 7 additions & 3 deletions test/jsdom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ describe.skipIf(globalThis.window !== undefined)('JsdomPageOpener', () => {
const pagePath = './test-modules/error.html'
const moduleUrl = pathToFileURL('./test-modules/error.js')

await expect(() => opener.open('unused', pagePath)).rejects
.toThrowError(`error importing modules from ${pagePath}: ` +
`Error: error importing ${moduleUrl.href}: Error: test error`)
const result = opener.open('unused', pagePath)

await expect(result).rejects.toThrowError(`opening ${pagePath}`)
await result.catch(err => {
expect(err.cause.message).toBe(`importing ${moduleUrl.href}`)
expect(err.cause.cause.message).toBe('test error')
})
})
})

0 comments on commit de8bc48

Please sign in to comment.