@@ -5,23 +5,20 @@ import { runCli } from '../../src'
55// Mock detect to see what options are passed to it
66const mocks = vi . hoisted ( ( ) => ( {
77 detectSpy : vi . fn ( ( ) => Promise . resolve ( 'npm' ) ) ,
8+ baseRunFnSpy : vi . fn < Runner > ( ( ) => Promise . resolve ( undefined ) ) ,
89} ) )
910vi . mock ( '../../src/detect' , ( ) => ( {
1011 detect : mocks . detectSpy ,
1112} ) )
1213
13- const baseRunFn : Runner = async ( ) => {
14- return undefined
15- }
16-
1714describe ( 'runCli' , ( ) => {
1815 afterEach ( ( ) => {
1916 vi . clearAllMocks ( )
2017 vi . unstubAllEnvs ( )
2118 } )
2219
2320 it ( 'run without errors' , async ( ) => {
24- const result = await runCli ( baseRunFn , { } )
21+ const result = await runCli ( mocks . baseRunFnSpy , { } )
2522 expect ( result ) . toBe ( undefined )
2623 } )
2724
@@ -34,24 +31,38 @@ describe('runCli', () => {
3431 } )
3532
3633 it ( 'calls detect with the correct options' , async ( ) => {
37- await runCli ( baseRunFn )
34+ await runCli ( mocks . baseRunFnSpy )
3835 expect ( mocks . detectSpy ) . toHaveBeenCalledWith ( { autoInstall : false , cwd : expect . any ( String ) } )
3936 } )
4037
4138 it ( 'detects environment options' , async ( ) => {
4239 vi . stubEnv ( 'NI_AUTO_INSTALL' , 'true' )
43- await runCli ( baseRunFn )
40+ await runCli ( mocks . baseRunFnSpy )
4441 expect ( mocks . detectSpy ) . toHaveBeenCalledWith ( { autoInstall : true , cwd : expect . any ( String ) } )
4542 } )
4643
4744 it ( 'accept options as input' , async ( ) => {
48- await runCli ( baseRunFn , { autoInstall : true , programmatic : true } )
45+ await runCli ( mocks . baseRunFnSpy , { autoInstall : true , programmatic : true } )
4946 expect ( mocks . detectSpy ) . toHaveBeenCalledWith ( { autoInstall : true , programmatic : true , cwd : expect . any ( String ) } )
5047 } )
5148
5249 it ( 'merges inputs and environment prioritizing inputs' , async ( ) => {
5350 vi . stubEnv ( 'NI_AUTO_INSTALL' , 'true' )
54- await runCli ( baseRunFn , { autoInstall : false , programmatic : true } )
51+ await runCli ( mocks . baseRunFnSpy , { autoInstall : false , programmatic : true } )
5552 expect ( mocks . detectSpy ) . toHaveBeenCalledWith ( { autoInstall : false , programmatic : true , cwd : expect . any ( String ) } )
5653 } )
54+
55+ describe ( 'onBeforeCommand' , ( ) => {
56+ it ( 'skips running the command when exit() is called' , async ( ) => {
57+ await runCli ( mocks . baseRunFnSpy , { onBeforeCommand : ( _args , ctx ) => ctx . exit ( ) } )
58+ expect ( mocks . baseRunFnSpy ) . not . toHaveBeenCalled ( )
59+ // https://github.com/antfu-collective/ni/issues/308
60+ expect ( mocks . detectSpy ) . not . toHaveBeenCalled ( )
61+ } )
62+
63+ it ( 'continues to run the command when exit() is not called' , async ( ) => {
64+ await runCli ( mocks . baseRunFnSpy , { onBeforeCommand : ( ) => Promise . resolve ( ) } )
65+ expect ( mocks . baseRunFnSpy ) . toHaveBeenCalledOnce ( )
66+ } )
67+ } )
5768} )
0 commit comments