Skip to content

Commit 4a0ff37

Browse files
committed
test: add parseNpmAlias unit tests
1 parent 6ad2761 commit 4a0ff37

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { parseNpmAlias } from '~/utils/npm/alias'
3+
4+
describe('parseNpmAlias', () => {
5+
it('returns null for a regular semver range (not an alias)', () => {
6+
expect(parseNpmAlias('^1.0.0')).toBeNull()
7+
})
8+
9+
it('returns null for an empty string', () => {
10+
expect(parseNpmAlias('')).toBeNull()
11+
})
12+
13+
it('returns null for a plain version number', () => {
14+
expect(parseNpmAlias('1.2.3')).toBeNull()
15+
})
16+
17+
it('returns null for a workspace protocol', () => {
18+
expect(parseNpmAlias('workspace:*')).toBeNull()
19+
})
20+
21+
it('parses a simple alias with version range', () => {
22+
expect(parseNpmAlias('npm:real-pkg@^1.0.0')).toEqual({
23+
name: 'real-pkg',
24+
range: '^1.0.0',
25+
})
26+
})
27+
28+
it('parses a scoped package alias', () => {
29+
expect(parseNpmAlias('npm:@scope/pkg@^1.0.0')).toEqual({
30+
name: '@scope/pkg',
31+
range: '^1.0.0',
32+
})
33+
})
34+
35+
it('parses an alias without a version range', () => {
36+
expect(parseNpmAlias('npm:real-pkg')).toEqual({
37+
name: 'real-pkg',
38+
range: '',
39+
})
40+
})
41+
42+
it('parses an alias with an exact version', () => {
43+
expect(parseNpmAlias('npm:real-pkg@2.0.0')).toEqual({
44+
name: 'real-pkg',
45+
range: '2.0.0',
46+
})
47+
})
48+
49+
it('parses a scoped package alias without version', () => {
50+
expect(parseNpmAlias('npm:@scope/pkg')).toEqual({
51+
name: '@scope/pkg',
52+
range: '',
53+
})
54+
})
55+
56+
it('strips the npm: prefix correctly', () => {
57+
const result = parseNpmAlias('npm:lodash@^4.0.0')
58+
expect(result).not.toBeNull()
59+
expect(result!.name).toBe('lodash')
60+
expect(result!.name).not.toContain('npm:')
61+
})
62+
})

0 commit comments

Comments
 (0)