@@ -17,11 +17,14 @@ jest.mock('ora');
1717jest . mock ( 'boxen' , ( ) => {
1818 return ( ) => 'success message' ;
1919} ) ;
20+ const spinner = {
21+ start : ( ) => spinner ,
22+ succeed : ( ) => spinner ,
23+ fail : ( ) => spinner ,
24+ clear : ( ) => spinner ,
25+ stop : ( ) => spinner
26+ } ;
2027ora . mockImplementation ( ( ) => {
21- const spinner = {
22- start : ( ) => spinner ,
23- succeed : ( ) => spinner
24- } ;
2528 return spinner ;
2629} ) ;
2730jest . mock ( '../src/create-twilio-function/install-dependencies.js' , ( ) => {
@@ -49,19 +52,19 @@ afterEach(async () => {
4952
5053describe ( 'createTwilioFunction' , ( ) => {
5154 beforeEach ( ( ) => {
52- nock ( 'https://raw.githubusercontent.com' )
53- . get ( '/github/gitignore/master/Node.gitignore' )
54- . reply ( 200 , '*.log\n.env' ) ;
55- } ) ;
56-
57- test ( 'it scaffolds a Twilio Function' , async ( ) => {
5855 inquirer . prompt = jest . fn ( ( ) =>
5956 Promise . resolve ( {
6057 accountSid : 'test-sid' ,
6158 authToken : 'test-auth-token'
6259 } )
6360 ) ;
6461
62+ nock ( 'https://raw.githubusercontent.com' )
63+ . get ( '/github/gitignore/master/Node.gitignore' )
64+ . reply ( 200 , '*.log\n.env' ) ;
65+ } ) ;
66+
67+ it ( 'scaffolds a Twilio Function' , async ( ) => {
6568 const name = 'test-function' ;
6669 await createTwilioFunction ( { name, path : './scratch' } ) ;
6770
@@ -95,14 +98,7 @@ describe('createTwilioFunction', () => {
9598 expect ( console . log ) . toHaveBeenCalledWith ( 'success message' ) ;
9699 } ) ;
97100
98- test ( 'it scaffolds a Twilio Function with a template' , async ( ) => {
99- inquirer . prompt = jest . fn ( ( ) =>
100- Promise . resolve ( {
101- accountSid : 'test-sid' ,
102- authToken : 'test-auth-token'
103- } )
104- ) ;
105-
101+ it ( 'scaffolds a Twilio Function with a template' , async ( ) => {
106102 const gitHubAPI = nock ( 'https://api.github.com' ) ;
107103 gitHubAPI
108104 . get ( '/repos/twilio-labs/function-templates/contents/blank?ref=next' )
@@ -185,17 +181,48 @@ describe('createTwilioFunction', () => {
185181 expect ( console . log ) . toHaveBeenCalledWith ( 'success message' ) ;
186182 } ) ;
187183
184+ it ( 'handles a missing template gracefully' , async ( ) => {
185+ const templateName = 'missing' ;
186+ const name = 'test-function' ;
187+ const gitHubAPI = nock ( 'https://api.github.com' ) ;
188+ gitHubAPI
189+ . get (
190+ `/repos/twilio-labs/function-templates/contents/${ templateName } ?ref=next`
191+ )
192+ . reply ( 404 ) ;
193+
194+ const fail = jest . spyOn ( spinner , 'fail' ) ;
195+
196+ await createTwilioFunction ( {
197+ name,
198+ path : './scratch' ,
199+ template : templateName
200+ } ) ;
201+
202+ expect . assertions ( 3 ) ;
203+
204+ expect ( fail ) . toHaveBeenCalledTimes ( 1 ) ;
205+ expect ( fail ) . toHaveBeenCalledWith (
206+ `The template "${ templateName } " doesn't exist`
207+ ) ;
208+ try {
209+ await stat ( `./scratch/${ name } ` ) ;
210+ } catch ( e ) {
211+ expect ( e . toString ( ) ) . toMatch ( 'no such file or directory' ) ;
212+ }
213+ } ) ;
214+
188215 it ( "doesn't scaffold if the target folder name already exists" , async ( ) => {
189216 const name = 'test-function' ;
190217 await mkdir ( './scratch/test-function' ) ;
191- console . error = jest . fn ( ) ;
218+ const fail = jest . spyOn ( spinner , 'fail' ) ;
192219
193220 await createTwilioFunction ( { name, path : './scratch' } ) ;
194221
195222 expect . assertions ( 4 ) ;
196223
197- expect ( console . error ) . toHaveBeenCalledTimes ( 1 ) ;
198- expect ( console . error ) . toHaveBeenCalledWith (
224+ expect ( fail ) . toHaveBeenCalledTimes ( 1 ) ;
225+ expect ( fail ) . toHaveBeenCalledWith (
199226 `A directory called '${ name } ' already exists. Please create your function in a new directory.`
200227 ) ;
201228 expect ( console . log ) . not . toHaveBeenCalled ( ) ;
@@ -211,14 +238,14 @@ describe('createTwilioFunction', () => {
211238 const name = 'test-function' ;
212239 const chmod = promisify ( fs . chmod ) ;
213240 await chmod ( './scratch' , 0o555 ) ;
214- console . error = jest . fn ( ) ;
241+ const fail = jest . spyOn ( spinner , 'fail' ) ;
215242
216243 await createTwilioFunction ( { name, path : './scratch' } ) ;
217244
218245 expect . assertions ( 4 ) ;
219246
220- expect ( console . error ) . toHaveBeenCalledTimes ( 1 ) ;
221- expect ( console . error ) . toHaveBeenCalledWith (
247+ expect ( fail ) . toHaveBeenCalledTimes ( 1 ) ;
248+ expect ( fail ) . toHaveBeenCalledWith (
222249 `You do not have permission to create files or directories in the path './scratch'.`
223250 ) ;
224251 expect ( console . log ) . not . toHaveBeenCalled ( ) ;
0 commit comments