Skip to content

Commit

Permalink
fix: don't throw unhandled error, if import was caught (#2494)
Browse files Browse the repository at this point in the history
* fix: don't throw unhandled error, if import was caught

* chore: add more tests
  • Loading branch information
sheremet-va committed Dec 13, 2022
1 parent 799f9a1 commit 0a87ebb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/vite-node/src/client.ts
Expand Up @@ -180,11 +180,12 @@ export class ViteNodeRunner {
const promise = this.directRequest(id, fsPath, callstack)
Object.assign(mod, { promise, evaluated: false })

promise.finally(() => {
try {
return await promise
}
finally {
mod.evaluated = true
})

return await promise
}
}

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

const file = cleanUrl(resolvedId || fsPath)
// console.log('file', file)

// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const url = pathToFileURL(file).href
const meta = { url }
Expand Down
8 changes: 8 additions & 0 deletions test/core/test/imports.test.ts
Expand Up @@ -53,3 +53,11 @@ test('dynamic import has null prototype', async () => {

expect(Object.getPrototypeOf(stringTimeoutMod)).toBe(null)
})

test('dynamic import throws an error', async () => {
const path = './some-unknown-path'
const imported = import(path)
await expect(imported).rejects.toThrowError(/Failed to load/)
// @ts-expect-error path does not exist
await expect(() => import('./some-unknown-path')).rejects.toThrowError(/Failed to load/)
})

0 comments on commit 0a87ebb

Please sign in to comment.