1
1
import chai from 'chai'
2
2
import nock from 'nock'
3
- import sinon from 'sinon'
4
3
import sinonChai from 'sinon-chai'
5
- import os from 'os'
6
4
import pkg from '@packages/root'
7
5
8
- import { createInstance } from '../../../../lib/cloud/api/create_instance'
6
+ import { createInstance as axiosCreateInstance , CreateInstanceRequestBody , CreateInstanceResponse } from '../../../../lib/cloud/api/create_instance'
7
+ import api from '../../../../lib/cloud/api'
9
8
10
9
chai . use ( sinonChai )
11
10
@@ -14,11 +13,14 @@ const { expect } = chai
14
13
const API_BASEURL = 'http://localhost:1234'
15
14
const OS_PLATFORM = 'linux'
16
15
16
+ const AXIOS_LABEL = 'axios createInstance'
17
+ const REQUEST_LABEL = 'request createInstance'
18
+
17
19
context ( 'API createInstance' , ( ) => {
18
20
let nocked
19
21
const runId = 'run-id-123'
20
22
21
- const instanceRequestData : Parameters < typeof createInstance > [ 1 ] = {
23
+ const instanceRequestData : CreateInstanceRequestBody = {
22
24
spec : null ,
23
25
groupId : 'groupId123' ,
24
26
machineId : 'machineId123' ,
@@ -32,7 +34,7 @@ context('API createInstance', () => {
32
34
} ,
33
35
}
34
36
35
- const instanceResponseData : Awaited < ReturnType < typeof createInstance > > = {
37
+ const instanceResponseData : CreateInstanceResponse = {
36
38
instanceId : 'instance-id-123' ,
37
39
claimedInstances : 0 ,
38
40
estimatedWallClockDuration : null ,
@@ -43,65 +45,96 @@ context('API createInstance', () => {
43
45
beforeEach ( ( ) => {
44
46
nocked = nock ( API_BASEURL )
45
47
. matchHeader ( 'x-cypress-run-id' , runId )
46
- // sinon stubbing on the `os` package doesn't work for `createInstance`
47
- //.matchHeader('x-os-name', OS_PLATFORM)
48
48
. matchHeader ( 'x-cypress-version' , pkg . version )
49
49
. post ( `/runs/${ runId } /instances` )
50
50
51
- sinon . stub ( os , 'platform' ) . returns ( OS_PLATFORM )
52
- } )
53
-
54
- afterEach ( ( ) => {
55
- ( os . platform as sinon . SinonStub ) . restore ( )
56
- } )
57
-
58
- describe ( 'when the request succeeds' , ( ) => {
59
- beforeEach ( ( ) => {
60
- nocked . reply ( 200 , instanceResponseData )
61
- } )
62
-
63
- it ( 'returns the created instance' , async ( ) => {
64
- const response = await createInstance ( runId , instanceRequestData )
65
-
66
- for ( let k in instanceResponseData ) {
67
- expect ( instanceResponseData [ k ] ) . to . eq ( response [ k ] )
68
- }
69
- } )
51
+ api . setPreflightResult ( { encrypt : false } )
70
52
} )
71
53
72
- describe ( 'when the request times out 3 times' , ( ) => {
73
- const timeout = 100
74
-
75
- beforeEach ( ( ) => {
76
- nocked
77
- . times ( 3 )
78
- . delayConnection ( 5000 )
79
- . reply ( 200 , instanceResponseData )
80
- } )
54
+ ; [
55
+ {
56
+ label : AXIOS_LABEL ,
57
+ fn : axiosCreateInstance ,
58
+ } ,
59
+ {
60
+ label : REQUEST_LABEL ,
61
+ fn : api . createInstance ,
62
+ } ,
63
+ ] . forEach ( function ( { label, fn : createInstance } ) {
64
+ describe ( label , function ( ) {
65
+ describe ( 'when the request succeeds' , ( ) => {
66
+ beforeEach ( ( ) => {
67
+ nocked . reply ( 200 , instanceResponseData )
68
+ } )
69
+
70
+ it ( 'returns the created instance' , async ( ) => {
71
+ const response = await createInstance ( runId , instanceRequestData )
72
+
73
+ for ( let k in instanceResponseData ) {
74
+ expect ( instanceResponseData [ k ] ) . to . eq ( response [ k ] )
75
+ }
76
+ } )
77
+ } )
81
78
82
- it ( 'throws an aggregate error' , ( ) => {
83
- return createInstance ( runId , instanceRequestData , timeout )
84
- . then ( ( ) => {
85
- throw new Error ( 'should have thrown here' )
86
- } ) . catch ( ( err ) => {
87
- for ( const error of err . errors ) {
88
- expect ( error . message ) . to . eq ( `timeout of ${ timeout } ms exceeded` )
89
- expect ( error . isApiError ) . to . be . true
79
+ describe ( 'when the request times out 4 times' , ( ) => {
80
+ const timeout = 10
81
+ let oldIntervals
82
+
83
+ beforeEach ( ( ) => {
84
+ oldIntervals = process . env . API_RETRY_INTERVALS
85
+ process . env . API_RETRY_INTERVALS = '0,0,0'
86
+ nocked
87
+ . times ( 4 )
88
+ . delayConnection ( 5000 )
89
+ . reply ( 200 , instanceResponseData )
90
+ } )
91
+
92
+ afterEach ( ( ) => {
93
+ process . env . API_RETRY_INTERVALS = oldIntervals
94
+ } )
95
+
96
+ // axios throws an AggregateError
97
+ if ( AXIOS_LABEL === label ) {
98
+ it ( 'throws an aggregate error' , ( ) => {
99
+ return createInstance ( runId , instanceRequestData , timeout )
100
+ . then ( ( ) => {
101
+ throw new Error ( 'should have thrown here' )
102
+ } ) . catch ( ( err ) => {
103
+ for ( const error of err . errors ) {
104
+ expect ( error . message ) . to . eq ( `timeout of ${ timeout } ms exceeded` )
105
+ expect ( error . isApiError ) . to . be . true
106
+ }
107
+ } )
108
+ } )
109
+ // request/promise throws the most recent error
110
+ } else {
111
+ it ( 'throws a tagged error' , async ( ) => {
112
+ let thrown : Error | undefined = undefined
113
+
114
+ try {
115
+ await createInstance ( runId , instanceRequestData , timeout )
116
+ } catch ( e ) {
117
+ thrown = e
118
+ }
119
+
120
+ expect ( thrown ) . not . to . be . undefined
121
+ expect ( ( thrown as Error & { isApiError ?: boolean } ) . isApiError ) . to . be . true
122
+ } )
90
123
}
91
124
} )
92
- } )
93
- } )
94
125
95
- describe ( 'when the request times out once and then succeeds' , ( ) => {
96
- beforeEach ( ( ) => {
97
- nocked . delayConnection ( 5000 ) . reply ( 200 , instanceResponseData )
98
- nocked . delayConnection ( 0 ) . reply ( 200 , instanceResponseData )
99
- } )
126
+ describe ( 'when the request times out once and then succeeds' , ( ) => {
127
+ beforeEach ( ( ) => {
128
+ nocked . delayConnection ( 5000 ) . reply ( 200 , instanceResponseData )
129
+ nocked . delayConnection ( 0 ) . reply ( 200 , instanceResponseData )
130
+ } )
100
131
101
- it ( 'returns the instance response data' , async ( ) => {
102
- const data = await createInstance ( runId , instanceRequestData , 100 )
132
+ it ( 'returns the instance response data' , async ( ) => {
133
+ const data = await createInstance ( runId , instanceRequestData , 100 )
103
134
104
- expect ( data ) . to . deep . eq ( instanceResponseData )
135
+ expect ( data ) . to . deep . eq ( instanceResponseData )
136
+ } )
137
+ } )
105
138
} )
106
139
} )
107
140
} )
0 commit comments