|
| 1 | +import path from 'node:path' |
| 2 | +import { cwd } from 'node:process' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { resolve } from '../../src' |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url) |
| 8 | + |
| 9 | +function expectResolve(source: string, found: boolean | string) { |
| 10 | + it(`${source} => ${found}`, () => { |
| 11 | + const result = resolve(source, __filename) |
| 12 | + if (typeof found === 'string') { |
| 13 | + expect(result.path).toBe(path.resolve(cwd(), found)) |
| 14 | + } else { |
| 15 | + expect(result.found).toBe(found) |
| 16 | + } |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +describe('builtin', () => { |
| 21 | + expectResolve('path', true) |
| 22 | + expectResolve('node:path', true) |
| 23 | +}) |
| 24 | + |
| 25 | +describe('modules', () => { |
| 26 | + expectResolve('vitest', true) |
| 27 | + expectResolve('lodash', false) |
| 28 | +}) |
| 29 | + |
| 30 | +describe('relative', () => { |
| 31 | + expectResolve('../../.gitignore', true) |
| 32 | + expectResolve('../../package.json', true) |
| 33 | + expectResolve('../../README.md', true) |
| 34 | + expectResolve('../../.github/dependabot.yml', true) |
| 35 | + expectResolve('../../vitest.config.ts', true) |
| 36 | + expectResolve('../../vitest.config', true) |
| 37 | + expectResolve('../../src/index', 'src/index.ts') |
| 38 | + expectResolve('../../src', 'src/index.ts') |
| 39 | + |
| 40 | + expectResolve('../inexistent.ts', false) |
| 41 | +}) |
| 42 | + |
| 43 | +describe('alias', () => { |
| 44 | + expectResolve('@/index.ts', true) |
| 45 | + expectResolve('@/index', true) |
| 46 | +}) |
0 commit comments