|
| 1 | +import { expect, it } from 'vitest' |
| 2 | + |
| 3 | +// @ts-expect-error untyped |
| 4 | +import * as notFound from '../src/external/not-found.js' |
| 5 | + |
| 6 | +it('path', async () => { |
| 7 | + await expect(() => notFound.importPath()).rejects.toMatchObject({ |
| 8 | + code: 'ERR_MODULE_NOT_FOUND', |
| 9 | + message: expect.stringMatching(/Cannot find module '.*?non-existing-path'/), |
| 10 | + }) |
| 11 | +}) |
| 12 | + |
| 13 | +// NodeJs's import.meta.resolve throws ERR_MODULE_NOT_FOUND error only this case. |
| 14 | +// For other cases, similar errors are fabricated by Vitest to mimic NodeJs's behavior. |
| 15 | +it('package', async () => { |
| 16 | + await expect(() => notFound.importPackage()).rejects.toMatchObject({ |
| 17 | + code: 'ERR_MODULE_NOT_FOUND', |
| 18 | + message: expect.stringContaining('Cannot find package \'@vitest/non-existing-package\''), |
| 19 | + }) |
| 20 | +}) |
| 21 | + |
| 22 | +it('builtin', async () => { |
| 23 | + await expect(() => notFound.importBuiltin()).rejects.toMatchObject({ |
| 24 | + code: 'ERR_MODULE_NOT_FOUND', |
| 25 | + message: 'Cannot find module \'node:non-existing-builtin\'', |
| 26 | + }) |
| 27 | +}) |
| 28 | + |
| 29 | +// this test fails before node 20.3.0 since it throws a different error (cf. https://github.com/nodejs/node/pull/47824) |
| 30 | +// > Only URLs with a scheme in: file and data are supported by the default ESM loader. Received protocol 'non-existing-namespace:' |
| 31 | +it('namespace', async () => { |
| 32 | + await expect(() => notFound.importNamespace()).rejects.toMatchObject({ |
| 33 | + code: 'ERR_MODULE_NOT_FOUND', |
| 34 | + message: 'Cannot find module \'non-existing-namespace:xyz\'', |
| 35 | + }) |
| 36 | +}) |
0 commit comments