-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vm): improve error when module is not found (#5053)
- Loading branch information
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export async function importPackage() { | ||
await import('@vitest/non-existing-package') | ||
} | ||
|
||
export async function importPath() { | ||
await import('./non-existing-path') | ||
} | ||
|
||
export async function importBuiltin() { | ||
await import('node:non-existing-builtin') | ||
} | ||
|
||
export async function importNamespace() { | ||
await import('non-existing-namespace:xyz') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect, it } from 'vitest' | ||
|
||
// @ts-expect-error untyped | ||
import * as notFound from '../src/external/not-found.js' | ||
|
||
it('path', async () => { | ||
await expect(() => notFound.importPath()).rejects.toMatchObject({ | ||
code: 'ERR_MODULE_NOT_FOUND', | ||
message: expect.stringMatching(/Cannot find module '.*?non-existing-path'/), | ||
}) | ||
}) | ||
|
||
// NodeJs's import.meta.resolve throws ERR_MODULE_NOT_FOUND error only this case. | ||
// For other cases, similar errors are fabricated by Vitest to mimic NodeJs's behavior. | ||
it('package', async () => { | ||
await expect(() => notFound.importPackage()).rejects.toMatchObject({ | ||
code: 'ERR_MODULE_NOT_FOUND', | ||
message: expect.stringContaining('Cannot find package \'@vitest/non-existing-package\''), | ||
}) | ||
}) | ||
|
||
it('builtin', async () => { | ||
await expect(() => notFound.importBuiltin()).rejects.toMatchObject({ | ||
code: 'ERR_MODULE_NOT_FOUND', | ||
message: 'Cannot find module \'node:non-existing-builtin\'', | ||
}) | ||
}) | ||
|
||
// this test fails before node 20.3.0 since it throws a different error (cf. https://github.com/nodejs/node/pull/47824) | ||
// > Only URLs with a scheme in: file and data are supported by the default ESM loader. Received protocol 'non-existing-namespace:' | ||
it('namespace', async () => { | ||
await expect(() => notFound.importNamespace()).rejects.toMatchObject({ | ||
code: 'ERR_MODULE_NOT_FOUND', | ||
message: 'Cannot find module \'non-existing-namespace:xyz\'', | ||
}) | ||
}) |