11import { describe , it , expect , vi , beforeEach } from "vitest" ;
22import { getArgumentValue , getArgumentValues } from "$lib/cli" ;
3+ import * as helpers from "$lib/helpers" ;
34
4- const expectExitError = "process.exit" ;
5- const mockProcessExit = vi . spyOn ( process , "exit" ) . mockImplementation ( ( ) => {
5+ const expectExitError = `process.exit unexpectedly called with "1"` ;
6+
7+ // Mock the exit function from helpers
8+ const mockExit = vi . spyOn ( helpers , "exit" ) . mockImplementation ( ( ) => {
69 throw new Error ( expectExitError ) ;
710} ) ;
811
9- const mockConsoleError = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
10-
1112describe ( "getArgumentValue" , ( ) => {
1213 beforeEach ( ( ) => {
1314 vi . resetAllMocks ( ) ;
@@ -19,36 +20,34 @@ describe("getArgumentValue", () => {
1920 const result = getArgumentValue ( "--config" ) ;
2021
2122 expect ( result ) . toBe ( "config.json" ) ;
22- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
23+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
2324 } ) ;
2425
25- it ( "should return null when argument does not exist" , ( ) => {
26+ it ( "should return undefined when argument does not exist" , ( ) => {
2627 process . argv = [ "node" , "script.js" , "--other" , "value" ] ;
2728
2829 const result = getArgumentValue ( "--config" ) ;
2930
3031 expect ( result ) . toBe ( undefined ) ;
31- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
32+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
3233 } ) ;
3334
34- it ( "should throw error when argument is at the end of argv" , ( ) => {
35+ it ( "should call exit when argument is at the end of argv" , ( ) => {
3536 process . argv = [ "node" , "script.js" , "--config" ] ;
3637
3738 expect ( ( ) => getArgumentValue ( "--config" ) ) . toThrow ( expectExitError ) ;
38- expect ( mockConsoleError ) . toHaveBeenCalledWith (
39+ expect ( mockExit ) . toHaveBeenCalledWith (
3940 'Error: Argument "--config" is provided but has no value.' ,
4041 ) ;
41- expect ( mockProcessExit ) . toHaveBeenCalledWith ( 1 ) ;
4242 } ) ;
4343
44- it ( "should throw error when argument value starts with --" , ( ) => {
44+ it ( "should call exit when argument value starts with --" , ( ) => {
4545 process . argv = [ "node" , "script.js" , "--config" , "--another-flag" ] ;
4646
4747 expect ( ( ) => getArgumentValue ( "--config" ) ) . toThrow ( expectExitError ) ;
48- expect ( mockConsoleError ) . toHaveBeenCalledWith (
48+ expect ( mockExit ) . toHaveBeenCalledWith (
4949 'Error: Argument "--config" is provided but has no value.' ,
5050 ) ;
51- expect ( mockProcessExit ) . toHaveBeenCalledWith ( 1 ) ;
5251 } ) ;
5352
5453 it ( "should return the correct value when multiple arguments exist" , ( ) => {
@@ -57,7 +56,7 @@ describe("getArgumentValue", () => {
5756 const result = getArgumentValue ( "--config" ) ;
5857
5958 expect ( result ) . toBe ( "config.json" ) ;
60- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
59+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
6160 } ) ;
6261
6362 it ( "should return the first occurrence when argument appears multiple times" , ( ) => {
@@ -66,7 +65,7 @@ describe("getArgumentValue", () => {
6665 const result = getArgumentValue ( "--config" ) ;
6766
6867 expect ( result ) . toBe ( "first.json" ) ;
69- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
68+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
7069 } ) ;
7170
7271 it ( "should handle arguments with special characters" , ( ) => {
@@ -75,7 +74,7 @@ describe("getArgumentValue", () => {
7574 const result = getArgumentValue ( "--path" ) ;
7675
7776 expect ( result ) . toBe ( "/path/to/file.json" ) ;
78- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
77+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
7978 } ) ;
8079
8180 it ( "should handle empty string values" , ( ) => {
@@ -84,7 +83,7 @@ describe("getArgumentValue", () => {
8483 const result = getArgumentValue ( "--config" ) ;
8584
8685 expect ( result ) . toBe ( "" ) ;
87- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
86+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
8887 } ) ;
8988
9089 it ( "should handle arguments with equals-like values" , ( ) => {
@@ -93,7 +92,7 @@ describe("getArgumentValue", () => {
9392 const result = getArgumentValue ( "--url" ) ;
9493
9594 expect ( result ) . toBe ( "http://example.com?param=value" ) ;
96- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
95+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
9796 } ) ;
9897
9998 it ( "should handle single character flags" , ( ) => {
@@ -102,17 +101,14 @@ describe("getArgumentValue", () => {
102101 const result = getArgumentValue ( "-v" ) ;
103102
104103 expect ( result ) . toBe ( "1.0.0" ) ;
105- expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
104+ expect ( mockExit ) . not . toHaveBeenCalled ( ) ;
106105 } ) ;
107106
108- it ( "should throw error when single character flag has no value" , ( ) => {
107+ it ( "should call exit when single character flag has no value" , ( ) => {
109108 process . argv = [ "node" , "script.js" , "" , "-v" ] ;
110109
111110 expect ( ( ) => getArgumentValue ( "-v" ) ) . toThrow ( expectExitError ) ;
112- expect ( mockConsoleError ) . toHaveBeenCalledWith (
113- 'Error: Argument "-v" is provided but has no value.' ,
114- ) ;
115- expect ( mockProcessExit ) . toHaveBeenCalledWith ( 1 ) ;
111+ expect ( mockExit ) . toHaveBeenCalledWith ( 'Error: Argument "-v" is provided but has no value.' ) ;
116112 } ) ;
117113} ) ;
118114
@@ -194,8 +190,11 @@ describe("getArgumentValues", () => {
194190 expect ( result ) . toEqual ( [ "urgent" ] ) ;
195191 } ) ;
196192
197- it ( "should handle mixed valid and invalid occurrences" , ( ) => {
193+ it ( "should call exit for mixed valid and invalid occurrences" , ( ) => {
198194 process . argv = [ "node" , "script.js" , "--files" , "file1.js" , "--files" ] ;
199195 expect ( ( ) => getArgumentValues ( "--files" ) ) . toThrow ( expectExitError ) ;
196+ expect ( mockExit ) . toHaveBeenCalledWith (
197+ 'Error: Argument "--files" is provided but has no value.' ,
198+ ) ;
200199 } ) ;
201200} ) ;
0 commit comments