Skip to content

Commit 9ac82cc

Browse files
committed
add test cases
1 parent 12ee36d commit 9ac82cc

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

server/utils/import-resolver.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,12 @@ export function resolveInternalImport(
321321
): ResolvedImport | null {
322322
const cleanSpecifier = specifier.replace(/^['"]|['"]$/g, '').trim()
323323

324-
if ((!cleanSpecifier.startsWith('#') && !cleanSpecifier.startsWith('~')) || !imports) {
324+
if (
325+
(!cleanSpecifier.startsWith('#') &&
326+
!cleanSpecifier.startsWith('~') &&
327+
!cleanSpecifier.startsWith('@')) ||
328+
!imports
329+
) {
325330
return null
326331
}
327332

test/unit/server/utils/import-resolver.spec.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ describe('createImportResolver', () => {
191191
})
192192
})
193193

194-
describe('resolveInternalSpecifier', () => {
194+
describe('resolveInternalImport', () => {
195195
it('resolves exact imports map matches to files in the package', () => {
196196
const files = new Set<string>(['dist/app/nuxt.js'])
197197

@@ -238,17 +238,62 @@ describe('resolveInternalSpecifier', () => {
238238
})
239239

240240
it('resolves prefix matches with extension resolution via guessInternalImportTarget', () => {
241-
const files = new Set<string>(['dist/app/nuxt.js'])
241+
const files = new Set<string>(['dist/app/components/button.js'])
242242

243243
const resolved = resolveInternalImport(
244-
'#app/nuxt',
244+
'#app/components/button.js',
245245
'dist/index.js',
246246
{
247247
'#app': './dist/app/index.js',
248248
},
249249
files,
250250
)
251251

252-
expect(resolved?.path).toBe('dist/app/nuxt.js')
252+
expect(resolved?.path).toBe('dist/app/components/button.js')
253+
})
254+
255+
it('resolves file that could not found in the files', () => {
256+
const files = new Set<string>(['dist/app/index.js'])
257+
258+
const resolved = resolveInternalImport(
259+
'#app/components/button.js',
260+
'dist/index.js',
261+
{
262+
'#app': './dist/app/index.js',
263+
},
264+
files,
265+
)
266+
267+
expect(resolved).toBeNull()
268+
})
269+
270+
it('resolves file that prefix is "~/"', () => {
271+
const files = new Set<string>(['dist/app/components/button.js'])
272+
273+
const resolved = resolveInternalImport(
274+
'~/app/components/button.js',
275+
'dist/index.js',
276+
{
277+
'~/app': './dist/app/index.js',
278+
},
279+
files,
280+
)
281+
282+
expect(resolved?.path).toBe('dist/app/components/button.js')
283+
})
284+
285+
it('resolves file that prefix is "@/"', () => {
286+
const files = new Set<string>(['dist/app/components/button.js'])
287+
288+
const resolved = resolveInternalImport(
289+
'@/app/components/button.js',
290+
'dist/index.js',
291+
{
292+
'@/app': './dist/app/index.js',
293+
},
294+
files,
295+
)
296+
297+
expect(resolved?.path).toBe('dist/app/components/button.js')
253298
})
254299
})

0 commit comments

Comments
 (0)