11import cli from '../cli' ;
22import * as commands from '../commands' ;
3+ import { globalOptions } from '../GlobalOptions' ;
34
45jest . mock ( '../commands' ) ;
56jest . mock ( '../utils/logger' ) ;
7+ jest . mock ( '../GlobalOptions' , ( ) => ( {
8+ globalOptions : {
9+ setFromFlags : jest . fn ( )
10+ }
11+ } ) ) ;
612
713const mockedCommands : any = commands ;
814
@@ -16,7 +22,6 @@ describe('cli', () => {
1622 mockedCommands . toHelpOptions . mockImplementationOnce ( ( ) => opts ) ;
1723 await cli ( [ '--help' ] ) ;
1824 expect ( mockedCommands . toHelpOptions ) . toHaveBeenCalledWith ( [ ] , {
19- '--' : [ ] ,
2025 help : true
2126 } ) ;
2227 expect ( commands . help ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -34,9 +39,7 @@ describe('cli', () => {
3439 const opts = { } ;
3540 mockedCommands . toInstallOptions . mockImplementationOnce ( ( ) => opts ) ;
3641 await cli ( [ ] ) ;
37- expect ( mockedCommands . toInstallOptions ) . toHaveBeenCalledWith ( [ ] , {
38- '--' : [ ]
39- } ) ;
42+ expect ( mockedCommands . toInstallOptions ) . toHaveBeenCalledWith ( [ ] , { } ) ;
4043 expect ( commands . install ) . toHaveBeenCalledTimes ( 1 ) ;
4144 expect ( commands . install ) . toHaveBeenCalledWith ( opts ) ;
4245 } ) ;
@@ -46,11 +49,134 @@ describe('cli', () => {
4649 mockedCommands . toAddOptions . mockImplementationOnce ( ( ) => opts ) ;
4750 await cli ( [ 'add' , 'foo' , '--dev' ] ) ;
4851 expect ( mockedCommands . toAddOptions ) . toHaveBeenCalledWith ( [ 'foo' ] , {
49- '--' : [ ] ,
5052 dev : true
5153 } ) ;
5254 expect ( commands . add ) . toHaveBeenCalledTimes ( 1 ) ;
5355 expect ( commands . add ) . toHaveBeenCalledWith ( opts ) ;
5456 } ) ;
5557 } ) ;
58+ describe ( 'global flags' , ( ) => {
59+ it ( 'should parse global flags before a command' , async ( ) => {
60+ const opts = { } ;
61+ mockedCommands . toAddOptions . mockImplementationOnce ( ( ) => opts ) ;
62+ await cli ( [ '--no-prefix' , 'add' , 'foo' ] ) ;
63+ expect ( mockedCommands . toAddOptions ) . toHaveBeenCalledWith ( [ 'foo' ] , {
64+ prefix : false
65+ } ) ;
66+ expect ( commands . add ) . toHaveBeenCalledTimes ( 1 ) ;
67+ expect ( commands . add ) . toHaveBeenCalledWith ( opts ) ;
68+ } ) ;
69+ it ( 'should parse global flags after a command' , async ( ) => {
70+ const opts = { } ;
71+ mockedCommands . toAddOptions . mockImplementationOnce ( ( ) => opts ) ;
72+ await cli ( [ 'add' , 'foo' , '--no-prefix' ] ) ;
73+ expect ( mockedCommands . toAddOptions ) . toHaveBeenCalledWith ( [ 'foo' ] , {
74+ prefix : false
75+ } ) ;
76+ expect ( commands . add ) . toHaveBeenCalledTimes ( 1 ) ;
77+ expect ( commands . add ) . toHaveBeenCalledWith ( opts ) ;
78+ } ) ;
79+ it ( 'should set global flags in globalOptions store' , async ( ) => {
80+ expect ( globalOptions . setFromFlags ) . not . toHaveBeenCalled ( ) ;
81+ await cli ( [ 'add' , 'foo' , '--no-prefix' ] ) ;
82+ expect ( globalOptions . setFromFlags ) . toHaveBeenCalledTimes ( 1 ) ;
83+ expect ( globalOptions . setFromFlags ) . toHaveBeenCalledWith ( {
84+ prefix : false
85+ } ) ;
86+ } ) ;
87+ } ) ;
88+ describe ( 'passing flags to run commands' , ( ) => {
89+ it ( 'bolt script --flag1 --flag2' , async ( ) => {
90+ const opts = { } ;
91+ mockedCommands . toRunOptions . mockImplementationOnce ( ( ) => opts ) ;
92+ await cli ( [ 'script' , '--flag1' , '--flag2' ] ) ;
93+ expect ( mockedCommands . toRunOptions ) . toHaveBeenCalledWith ( [ 'script' ] , {
94+ flag1 : true ,
95+ flag2 : true
96+ } ) ;
97+ expect ( commands . run ) . toHaveBeenCalledTimes ( 1 ) ;
98+ expect ( commands . run ) . toHaveBeenCalledWith ( opts ) ;
99+ } ) ;
100+ it ( 'bolt run script --flag1 --flag2' , async ( ) => {
101+ const opts = { } ;
102+ mockedCommands . toRunOptions . mockImplementationOnce ( ( ) => opts ) ;
103+ await cli ( [ 'run' , 'script' , '--flag1' , '--flag2' ] ) ;
104+ expect ( mockedCommands . toRunOptions ) . toHaveBeenCalledWith ( [ 'script' ] , {
105+ flag1 : true ,
106+ flag2 : true
107+ } ) ;
108+ expect ( commands . run ) . toHaveBeenCalledTimes ( 1 ) ;
109+ expect ( commands . run ) . toHaveBeenCalledWith ( opts ) ;
110+ } ) ;
111+ it ( 'bolt p run script --flag1 --flag2' , async ( ) => {
112+ const opts = { } ;
113+ mockedCommands . toProjectRunOptions . mockImplementationOnce ( ( ) => opts ) ;
114+ await cli ( [ 'p' , 'run' , 'script' , '--flag1' , '--flag2' ] ) ;
115+ expect ( mockedCommands . toProjectRunOptions ) . toHaveBeenCalledWith (
116+ [ 'script' ] ,
117+ {
118+ flag1 : true ,
119+ flag2 : true
120+ }
121+ ) ;
122+ expect ( commands . projectRun ) . toHaveBeenCalledTimes ( 1 ) ;
123+ expect ( commands . projectRun ) . toHaveBeenCalledWith ( opts ) ;
124+ } ) ;
125+ it ( 'bolt w my-package run script --flag1 --flag2' , async ( ) => {
126+ const opts = { } ;
127+ mockedCommands . toWorkspaceRunOptions . mockImplementationOnce ( ( ) => opts ) ;
128+ await cli ( [ 'w' , 'my-package' , 'run' , 'script' , '--flag1' , '--flag2' ] ) ;
129+ expect ( mockedCommands . toWorkspaceRunOptions ) . toHaveBeenCalledWith (
130+ [ 'my-package' , 'script' ] ,
131+ {
132+ flag1 : true ,
133+ flag2 : true
134+ }
135+ ) ;
136+ expect ( commands . workspaceRun ) . toHaveBeenCalledTimes ( 1 ) ;
137+ expect ( commands . workspaceRun ) . toHaveBeenCalledWith ( opts ) ;
138+ } ) ;
139+ it ( 'bolt ws --only="my-package" run script -- --flag1 --flag2' , async ( ) => {
140+ const opts = { } ;
141+ mockedCommands . toWorkspacesRunOptions . mockImplementationOnce ( ( ) => opts ) ;
142+ await cli ( [
143+ 'ws' ,
144+ '--only=my-package' ,
145+ 'run' ,
146+ 'script' ,
147+ '--' ,
148+ '--flag1' ,
149+ '--flag2'
150+ ] ) ;
151+ expect ( mockedCommands . toWorkspacesRunOptions ) . toHaveBeenCalledWith (
152+ [ 'script' ] ,
153+ {
154+ '--' : [ '--flag1' , '--flag2' ] ,
155+ only : 'my-package'
156+ }
157+ ) ;
158+ expect ( commands . workspacesRun ) . toHaveBeenCalledTimes ( 1 ) ;
159+ expect ( commands . workspacesRun ) . toHaveBeenCalledWith ( opts ) ;
160+ } ) ;
161+ it ( 'bolt ws exec --only="my-package" -- yarn script --flag1 --flag2' , async ( ) => {
162+ const opts = { } ;
163+ mockedCommands . toWorkspacesExecOptions . mockImplementationOnce ( ( ) => opts ) ;
164+ await cli ( [
165+ 'ws' ,
166+ 'exec' ,
167+ '--only=my-package' ,
168+ '--' ,
169+ 'yarn' ,
170+ 'script' ,
171+ '--flag1' ,
172+ '--flag2'
173+ ] ) ;
174+ expect ( mockedCommands . toWorkspacesExecOptions ) . toHaveBeenCalledWith ( [ ] , {
175+ '--' : [ 'yarn' , 'script' , '--flag1' , '--flag2' ] ,
176+ only : 'my-package'
177+ } ) ;
178+ expect ( commands . workspacesExec ) . toHaveBeenCalledTimes ( 1 ) ;
179+ expect ( commands . workspacesExec ) . toHaveBeenCalledWith ( opts ) ;
180+ } ) ;
181+ } ) ;
56182} ) ;
0 commit comments