shadcn-vue/packages/cli/test/utils/resolve-import.test.ts
Sadegh Barati 0e84af73de
feat: use unjs modules and improve cli from main shadcn-ui source, custom ui dir (#324)
* feat: add devDeps, add nypm for installing deps

* feat: custom ui dir

* refactor: use consola instead of chalk

* test: ui alias

* refactor: import { z } from 'zod' instead of *, replace node:path with pathe

* chore: add components name to `configFile` option

* chore: update `c12` which fix json5 parse issue

and it also supports .config directory

* chore: update `https-proxy-agent`

* fix: await until dependencies are installed then run detypes process

* feat: add tailwind prefix

* test: tw-prefix snapshot

* chore: add prefix option to init

* test: apply prefix

* fix: tw-prefix parse wrongly

* chore: hide prefix temporarily

---------

Co-authored-by: zernonia <zernonia@gmail.com>
2024-03-06 05:38:19 +03:30

82 lines
2.4 KiB
TypeScript

import path from 'pathe'
import { type ConfigLoaderSuccessResult, loadConfig } from 'tsconfig-paths'
import { expect, test } from 'vitest'
import { resolveImport } from '../../src/utils/resolve-import'
test('resolve import', async () => {
expect(
resolveImport('@/foo/bar', {
absoluteBaseUrl: '/Users/shadcn/Projects/foobar',
paths: {
'@/*': ['./src/*'],
'~/components/*': ['./src/components/*'],
'~/lib': ['./src/lib'],
},
}),
).toEqual('/Users/shadcn/Projects/foobar/src/foo/bar')
expect(
resolveImport('~/components/foo/bar/baz', {
absoluteBaseUrl: '/Users/shadcn/Projects/foobar',
paths: {
'@/*': ['./src/*'],
'~/components/*': ['./src/components/*'],
'~/lib': ['./src/lib'],
},
}),
).toEqual('/Users/shadcn/Projects/foobar/src/components/foo/bar/baz')
expect(
resolveImport('components/foo/bar', {
absoluteBaseUrl: '/Users/shadcn/Projects/foobar',
paths: {
'components/*': ['./src/app/components/*'],
'ui/*': ['./src/ui/primities/*'],
'lib': ['./lib'],
},
}),
).toEqual('/Users/shadcn/Projects/foobar/src/app/components/foo/bar')
expect(
resolveImport('lib/utils', {
absoluteBaseUrl: '/Users/shadcn/Projects/foobar',
paths: {
'components/*': ['./src/app/components/*'],
'ui/*': ['./src/ui/primities/*'],
'lib': ['./lib'],
},
}),
).toEqual('/Users/shadcn/Projects/foobar/lib/utils')
})
test('resolve import with base url', async () => {
const cwd = path.resolve(__dirname, '../fixtures/with-base-url')
const config = (loadConfig(cwd)) as ConfigLoaderSuccessResult
expect(resolveImport('@/components/ui', config)).toEqual(
path.resolve(cwd, 'components/ui'),
)
expect(resolveImport('@/lib/utils', config)).toEqual(
path.resolve(cwd, 'lib/utils'),
)
expect(resolveImport('foo/bar', config)).toEqual(
path.resolve(cwd, 'foo/bar'),
)
})
test('resolve import without base url', async () => {
const cwd = path.resolve(__dirname, '../fixtures/without-base-url')
const config = (loadConfig(cwd)) as ConfigLoaderSuccessResult
expect(resolveImport('~/components/ui', config)).toEqual(
path.resolve(cwd, 'components/ui'),
)
expect(resolveImport('~/lib/utils', config)).toEqual(
path.resolve(cwd, 'lib/utils'),
)
expect(resolveImport('foo/bar', config)).toEqual(
path.resolve(cwd, 'foo/bar'),
)
})