1010 * governing permissions and limitations under the License.
1111 */
1212
13- const fetch = require ( 'node-fetch' )
1413const inquirer = require ( 'inquirer' )
1514const TheCommand = require ( '../../src/commands/discover' )
1615const { stdout } = require ( 'stdout-stderr' )
@@ -20,7 +19,6 @@ jest.mock('inquirer')
2019let command
2120
2221beforeEach ( ( ) => {
23- fetch . resetMocks ( )
2422 command = new TheCommand ( [ ] )
2523 command . config = {
2624 commands : [ { pluginName : '@adobe/aio-cli-plugin-baz' } ] ,
@@ -44,13 +42,14 @@ describe('sorting', () => {
4442 ]
4543 }
4644 beforeEach ( ( ) => {
47- fetch . mockResponseOnce ( JSON . stringify ( expectedResult ) )
45+ setFetchMock ( true , expectedResult )
4846 } )
4947
5048 test ( 'unknown sort-field' , async ( ) => {
51- fetch . mockResponseOnce ( JSON . stringify ( {
49+ setFetchMock ( true , {
5250 objects : [ ]
53- } ) )
51+ } )
52+
5453 command . argv = [ '--sort-field' , 'unknown' ]
5554 await expect ( command . run ( ) ) . rejects . toThrow ( 'Expected --sort-field=' )
5655 } )
@@ -100,7 +99,7 @@ test('interactive install', async () => {
10099 { package : { name : '@adobe/aio-cli-plugin-baz' , description : 'some baz' , version : '1.0.2' , date : dayAfter } }
101100 ]
102101 }
103- fetch . mockResponseOnce ( JSON . stringify ( expectedResult ) )
102+ setFetchMock ( true , expectedResult )
104103
105104 command . argv = [ '-i' ]
106105 inquirer . prompt = jest . fn ( ) . mockResolvedValue ( {
@@ -121,7 +120,7 @@ test('interactive install - no choices', async () => {
121120 { package : { name : '@adobe/aio-cli-plugin-baz' , description : 'some baz' , version : '1.0.2' , date : now } }
122121 ]
123122 }
124- fetch . mockResponseOnce ( JSON . stringify ( expectedResult ) )
123+ setFetchMock ( true , expectedResult )
125124
126125 command . argv = [ '-i' ]
127126 inquirer . prompt = jest . fn ( ) . mockResolvedValue ( {
@@ -132,7 +131,9 @@ test('interactive install - no choices', async () => {
132131} )
133132
134133test ( 'json result error' , async ( ) => {
135- fetch . mockResponse ( )
134+ const errorMessage = 'Invalid JSON response'
135+ setFetchMock ( false , errorMessage )
136+
136137 command . argv = [ ]
137- await expect ( command . run ( ) ) . rejects . toThrow ( 'FetchError: invalid json response body' )
138+ await expect ( command . run ( ) ) . rejects . toThrow ( errorMessage )
138139} )
0 commit comments