Skip to content

Commit 0a87ebb

Browse files
authoredDec 13, 2022
fix: don't throw unhandled error, if import was caught (#2494)
* fix: don't throw unhandled error, if import was caught * chore: add more tests
1 parent 799f9a1 commit 0a87ebb

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed
 

‎packages/vite-node/src/client.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ export class ViteNodeRunner {
180180
const promise = this.directRequest(id, fsPath, callstack)
181181
Object.assign(mod, { promise, evaluated: false })
182182

183-
promise.finally(() => {
183+
try {
184+
return await promise
185+
}
186+
finally {
184187
mod.evaluated = true
185-
})
186-
187-
return await promise
188+
}
188189
}
189190

190191
/** @internal */
@@ -272,7 +273,7 @@ export class ViteNodeRunner {
272273
throw new Error(`[vite-node] Failed to load ${id}`)
273274

274275
const file = cleanUrl(resolvedId || fsPath)
275-
// console.log('file', file)
276+
276277
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
277278
const url = pathToFileURL(file).href
278279
const meta = { url }

‎test/core/test/imports.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ test('dynamic import has null prototype', async () => {
5353

5454
expect(Object.getPrototypeOf(stringTimeoutMod)).toBe(null)
5555
})
56+
57+
test('dynamic import throws an error', async () => {
58+
const path = './some-unknown-path'
59+
const imported = import(path)
60+
await expect(imported).rejects.toThrowError(/Failed to load/)
61+
// @ts-expect-error path does not exist
62+
await expect(() => import('./some-unknown-path')).rejects.toThrowError(/Failed to load/)
63+
})

0 commit comments

Comments
 (0)
Please sign in to comment.