1+ import chalk from "chalk"
12import massarg from "../src"
23import { OptionDef } from "../src/types"
34
@@ -143,9 +144,17 @@ describe("Options", () => {
143144 } )
144145
145146 describe ( "required" , ( ) => {
147+ const mockProcessExit = jest . spyOn ( process , "exit" ) . mockImplementation ( ( code ) => {
148+ throw new Error ( `Process.exit(${ code } )` ) // Forces the code to throw instead of exit
149+ } )
150+ beforeEach ( ( ) => {
151+ mockProcessExit . mockClear ( )
152+ } )
153+
146154 test ( "should throw on missing required value" , ( ) => {
147155 const mockConsoleError = jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => void 0 )
148156 const mockConsoleLog = jest . spyOn ( console , "log" ) . mockImplementation ( ( ) => void 0 )
157+
149158 expect ( ( ) =>
150159 massarg ( )
151160 . option ( {
@@ -154,7 +163,10 @@ describe("Options", () => {
154163 required : true ,
155164 } )
156165 . parse ( [ "--not-number" , "abcdefg" ] )
157- ) . toThrow ( "Option: `number` is required, but was not defined. Try using: `--number {value}`" )
166+ ) . toThrow ( "Process.exit(1)" )
167+ expect ( mockConsoleError ) . toBeCalledWith (
168+ chalk . red `Option: \`number\` is required, but was not defined. Try using: \`--number \{value\}\``
169+ )
158170 mockConsoleError . mockRestore ( )
159171 mockConsoleLog . mockRestore ( )
160172 } )
@@ -196,7 +208,10 @@ describe("Options", () => {
196208 run : ( ) => void 0 ,
197209 } )
198210 . parse ( [ "cmd" ] )
199- ) . toThrow ( "Option: `number` is required for command: `cmd`, but was not defined. Try using: `--number {value}`" )
211+ ) . toThrow ( "Process.exit(1)" )
212+ expect ( mockConsoleError ) . toBeCalledWith (
213+ chalk . red `Option: \`number\` is required for command: \`cmd\`, but was not defined. Try using: \`--number \{value\}\``
214+ )
200215 mockConsoleError . mockRestore ( )
201216 mockConsoleLog . mockRestore ( )
202217 } )
0 commit comments