10
10
* governing permissions and limitations under the License.
11
11
*/
12
12
13
- const fetch = require ( 'node-fetch' )
14
13
const inquirer = require ( 'inquirer' )
15
14
const TheCommand = require ( '../../src/commands/discover' )
16
15
const { stdout } = require ( 'stdout-stderr' )
@@ -20,7 +19,6 @@ jest.mock('inquirer')
20
19
let command
21
20
22
21
beforeEach ( ( ) => {
23
- fetch . resetMocks ( )
24
22
command = new TheCommand ( [ ] )
25
23
command . config = {
26
24
commands : [ { pluginName : '@adobe/aio-cli-plugin-baz' } ] ,
@@ -44,13 +42,14 @@ describe('sorting', () => {
44
42
]
45
43
}
46
44
beforeEach ( ( ) => {
47
- fetch . mockResponseOnce ( JSON . stringify ( expectedResult ) )
45
+ setFetchMock ( true , expectedResult )
48
46
} )
49
47
50
48
test ( 'unknown sort-field' , async ( ) => {
51
- fetch . mockResponseOnce ( JSON . stringify ( {
49
+ setFetchMock ( true , {
52
50
objects : [ ]
53
- } ) )
51
+ } )
52
+
54
53
command . argv = [ '--sort-field' , 'unknown' ]
55
54
await expect ( command . run ( ) ) . rejects . toThrow ( 'Expected --sort-field=' )
56
55
} )
@@ -100,7 +99,7 @@ test('interactive install', async () => {
100
99
{ package : { name : '@adobe/aio-cli-plugin-baz' , description : 'some baz' , version : '1.0.2' , date : dayAfter } }
101
100
]
102
101
}
103
- fetch . mockResponseOnce ( JSON . stringify ( expectedResult ) )
102
+ setFetchMock ( true , expectedResult )
104
103
105
104
command . argv = [ '-i' ]
106
105
inquirer . prompt = jest . fn ( ) . mockResolvedValue ( {
@@ -121,7 +120,7 @@ test('interactive install - no choices', async () => {
121
120
{ package : { name : '@adobe/aio-cli-plugin-baz' , description : 'some baz' , version : '1.0.2' , date : now } }
122
121
]
123
122
}
124
- fetch . mockResponseOnce ( JSON . stringify ( expectedResult ) )
123
+ setFetchMock ( true , expectedResult )
125
124
126
125
command . argv = [ '-i' ]
127
126
inquirer . prompt = jest . fn ( ) . mockResolvedValue ( {
@@ -132,7 +131,9 @@ test('interactive install - no choices', async () => {
132
131
} )
133
132
134
133
test ( 'json result error' , async ( ) => {
135
- fetch . mockResponse ( )
134
+ const errorMessage = 'Invalid JSON response'
135
+ setFetchMock ( false , errorMessage )
136
+
136
137
command . argv = [ ]
137
- await expect ( command . run ( ) ) . rejects . toThrow ( 'FetchError: invalid json response body' )
138
+ await expect ( command . run ( ) ) . rejects . toThrow ( errorMessage )
138
139
} )
0 commit comments