1+ import { basename , resolve } from 'node:path'
12import { beforeEach , describe , expect , it } from 'vitest'
23
34import { normalizeOptions } from '../src/command-line.js'
5+ import {
6+ sanitizePackageName ,
7+ getCurrentDirectoryName ,
8+ } from '../src/utils.js'
49import {
510 __testRegisterFramework ,
611 __testClearFrameworks ,
@@ -10,12 +15,58 @@ beforeEach(() => {
1015 __testClearFrameworks ( )
1116} )
1217
18+ describe ( 'sanitizePackageName' , ( ) => {
19+ it ( 'should convert to lowercase' , ( ) => {
20+ expect ( sanitizePackageName ( 'MyProject' ) ) . toBe ( 'myproject' )
21+ } )
22+
23+ it ( 'should replace spaces with hyphens' , ( ) => {
24+ expect ( sanitizePackageName ( 'my project' ) ) . toBe ( 'my-project' )
25+ } )
26+
27+ it ( 'should replace underscores with hyphens' , ( ) => {
28+ expect ( sanitizePackageName ( 'my_project' ) ) . toBe ( 'my-project' )
29+ } )
30+
31+ it ( 'should remove invalid characters' , ( ) => {
32+ expect ( sanitizePackageName ( 'my@project!' ) ) . toBe ( 'myproject' )
33+ } )
34+
35+ it ( 'should ensure it starts with a letter' , ( ) => {
36+ expect ( sanitizePackageName ( '123project' ) ) . toBe ( 'project' )
37+ expect ( sanitizePackageName ( '_myproject' ) ) . toBe ( 'myproject' )
38+ } )
39+
40+ it ( 'should collapse multiple hyphens' , ( ) => {
41+ expect ( sanitizePackageName ( 'my--project' ) ) . toBe ( 'my-project' )
42+ } )
43+
44+ it ( 'should remove trailing hyphen' , ( ) => {
45+ expect ( sanitizePackageName ( 'myproject-' ) ) . toBe ( 'myproject' )
46+ } )
47+ } )
48+
49+ describe ( 'getCurrentDirectoryName' , ( ) => {
50+ it ( 'should return the basename of the current working directory' , ( ) => {
51+ expect ( getCurrentDirectoryName ( ) ) . toBe ( basename ( process . cwd ( ) ) )
52+ } )
53+ } )
54+
1355describe ( 'normalizeOptions' , ( ) => {
1456 it ( 'should return undefined if project name is not provided' , async ( ) => {
1557 const options = await normalizeOptions ( { } )
1658 expect ( options ) . toBeUndefined ( )
1759 } )
1860
61+ it ( 'should handle "." as project name by using sanitized current directory name' , async ( ) => {
62+ const options = await normalizeOptions ( {
63+ projectName : '.' ,
64+ } )
65+ const expectedName = sanitizePackageName ( getCurrentDirectoryName ( ) )
66+ expect ( options ?. projectName ) . toBe ( expectedName )
67+ expect ( options ?. targetDir ) . toBe ( resolve ( process . cwd ( ) ) )
68+ } )
69+
1970 it ( 'should return enable typescript based on the framework' , async ( ) => {
2071 const jsOptions = await normalizeOptions ( {
2172 projectName : 'test' ,
0 commit comments