Skip to content

Commit 9aca81a

Browse files
tamil777selvanerwinheitzmanchristian-bromann
committed
V9 migrate from got to fetch (#11942)
* V9 drop support for Node.js 16 (#11493) * (internal): drop support for Node.js 16 update engines of package.json files * (internal): drop support for Node.js 16 update CONTRIBUTING.md with Node.js 20 as recommendation * (internal): drop support for Node.js 16 update github workflows * (internal): drop support for Node.js 16 update external packages that dropped the support already * (internal): drop support for Node.js 16 resolve PR feedback * Update packages/webdriver/package.json * Update packages/wdio-cucumber-framework/package.json * Update package.json --------- Co-authored-by: Christian Bromann <git@bromann.dev> * (webdriverio): merge isDisplayed and isDisplayedWithinViewport (#11857) * (webdriverio): merge isDisplayed and isDisplayedWithinViewport * allow to specify withinViewport check in waitForDisplayed * V9 drop support for Node.js 16 (#11493) * (internal): drop support for Node.js 16 update engines of package.json files * (internal): drop support for Node.js 16 update CONTRIBUTING.md with Node.js 20 as recommendation * (internal): drop support for Node.js 16 update github workflows * (internal): drop support for Node.js 16 update external packages that dropped the support already * (internal): drop support for Node.js 16 resolve PR feedback * Update packages/webdriver/package.json * Update packages/wdio-cucumber-framework/package.json * Update package.json --------- Co-authored-by: Christian Bromann <git@bromann.dev> * (webdriverio): merge isDisplayed and isDisplayedWithinViewport (#11857) * (webdriverio): merge isDisplayed and isDisplayedWithinViewport * allow to specify withinViewport check in waitForDisplayed * Migrating from got to fetch * Migrate got to fetch in packages * migrate got to fetch & update unit test * doc update for proxy setup * rebase and migrate got to fetch in browserstack * Migrate hostname from 0.0.0.0 to localhost * fix unit test by removing headers in safari driver * fix smoke test: update nock to beta * fix launch test for firefox * fix unit test for startGeckodriver * fix firefox executions in node 18 * Incorporating review comments * fix component tests * cleaning up RequestLibOptions * Removing RequestLibOptions & incorporating other review comments * use process from globalThis --------- Co-authored-by: Erwin Heitzman <15839059+erwinheitzman@users.noreply.github.com> Co-authored-by: Christian Bromann <git@bromann.dev>
1 parent f115100 commit 9aca81a

170 files changed

Lines changed: 7050 additions & 3303 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

__mocks__/got.ts renamed to __mocks__/fetch.ts

Lines changed: 92 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vi } from 'vitest'
22

3-
export const ELEMENT_KEY = 'element-6066-11e4-a52e-4f735466cecf'
4-
export const SHADOW_ELEMENT_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'
3+
const ELEMENT_KEY = 'element-6066-11e4-a52e-4f735466cecf'
4+
const SHADOW_ELEMENT_KEY = 'shadow-6066-11e4-a52e-4f735466cecf'
55

66
let manualMockResponse: any
77

@@ -15,6 +15,24 @@ const genericSubElementId = 'some-sub-elem-321'
1515
const genericSubSubElementId = 'some-sub-sub-elem-231'
1616
const genericShadowElementId = 'some-shadow-elem-123'
1717
const genericSubShadowElementId = 'some-shadow-sub-elem-321'
18+
19+
/**
20+
* Transform the specified property of each object in the collection by replacing 'mockFunction' with a predefined function (vi.fn()).
21+
* This is intended to ensure that, when converting the request body to a string, functions are retained and not omitted.
22+
* @param collection - An array of objects to process.
23+
* @returns A new array with updated objects.
24+
*/
25+
const transformPropertyWithMockFunction = (collection: any[]) => {
26+
return collection.map(item => {
27+
for (const prop in item) {
28+
if (item[prop] && item[prop] === 'mockFunction') {
29+
item[prop] = vi.fn()
30+
}
31+
}
32+
return item
33+
})
34+
}
35+
1836
const requestMock: any = vi.fn().mockImplementation((uri, params) => {
1937
let value: any = {}
2038
let jsonwpMode = false
@@ -36,13 +54,24 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
3654
if (!(uri as URL).pathname.match(pattern)) {
3755
continue
3856
}
39-
return response
57+
return Response.json(response)
58+
}
59+
60+
let body: any = params?.body
61+
62+
try {
63+
body = body && JSON.parse(body.toString())
64+
} catch {
65+
return Response.json({}, {
66+
status: 422,
67+
statusText: 'Unprocessable Entity'
68+
})
4069
}
4170

4271
if (
43-
params.json &&
44-
params.json.capabilities &&
45-
params.json.capabilities.alwaysMatch.jsonwpMode
72+
body &&
73+
body.capabilities &&
74+
body.capabilities.alwaysMatch.jsonwpMode
4675
) {
4776
jsonwpMode = true
4877
sessionResponse = {
@@ -52,61 +81,61 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
5281
}
5382

5483
if (
55-
params.json &&
56-
params.json.capabilities &&
57-
params.json.capabilities.alwaysMatch.mobileMode
84+
body &&
85+
body.capabilities &&
86+
body.capabilities.alwaysMatch.mobileMode
5887
) {
5988
sessionResponse.capabilities.deviceName = 'iNode'
6089
}
6190

6291
if (
63-
params.json &&
64-
params.json.capabilities &&
65-
params.json.capabilities.alwaysMatch.keepBrowserName
92+
body &&
93+
body.capabilities &&
94+
body.capabilities.alwaysMatch.keepBrowserName
6695
) {
67-
sessionResponse.capabilities.browserName = params.json.capabilities.alwaysMatch.browserName
96+
sessionResponse.capabilities.browserName = body.capabilities.alwaysMatch.browserName
6897
}
6998

7099
if (
71-
params.json &&
72-
params.json.desiredCapabilities &&
73-
params.json.desiredCapabilities['sauce:options']
100+
body &&
101+
body.desiredCapabilities &&
102+
body.desiredCapabilities['sauce:options']
74103
) {
75-
sessionResponse.capabilities['sauce:options'] = params.json.desiredCapabilities['sauce:options']
104+
sessionResponse.capabilities['sauce:options'] = body.desiredCapabilities['sauce:options']
76105
}
77106

78107
switch (uri.pathname) {
79108
case path:
80109
value = sessionResponse
81110

82-
if (params.json.capabilities.alwaysMatch.browserName && params.json.capabilities.alwaysMatch.browserName.includes('noW3C')) {
111+
if (body.capabilities.alwaysMatch.browserName && body.capabilities.alwaysMatch.browserName.includes('noW3C')) {
83112
value.desiredCapabilities = { browserName: 'mockBrowser' }
84113
delete value.capabilities
85114
}
86115

87-
if (params.json.capabilities.alwaysMatch.browserName && params.json.capabilities.alwaysMatch.browserName.includes('devtools')) {
116+
if (body.capabilities.alwaysMatch.browserName && body.capabilities.alwaysMatch.browserName.includes('devtools')) {
88117
value.capabilities['goog:chromeOptions'] = {
89118
debuggerAddress: 'localhost:1234'
90119
}
91120
}
92121

93-
if (params.json.capabilities.alwaysMatch.platformName && params.json.capabilities.alwaysMatch.platformName.includes('iOS')) {
122+
if (body.capabilities.alwaysMatch.platformName && body.capabilities.alwaysMatch.platformName.includes('iOS')) {
94123
value.capabilities.platformName = 'iOS'
95124
}
96125

97126
break
98127
case `/session/${sessionId}/element`:
99-
if (params.json && params.json.value === '#nonexisting') {
128+
if (body && body.value === '#nonexisting') {
100129
value = { elementId: null }
101130
break
102131
}
103132

104-
if (params.json && params.json.value === 'html') {
133+
if (body && body.value === 'html') {
105134
value = { [ELEMENT_KEY]: 'html-element' }
106135
break
107136
}
108137

109-
if (params.json && params.json.value === '#slowRerender') {
138+
if (body && body.value === '#slowRerender') {
110139
++requestMock.retryCnt
111140
if (requestMock.retryCnt === 2) {
112141
++requestMock.retryCnt
@@ -207,47 +236,47 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
207236
break
208237
case `/session/${sessionId}/execute`:
209238
case `/session/${sessionId}/execute/sync`: {
210-
const script = Function(params.json.script)
211-
const args = params.json.args.map((arg: any) => (arg && (arg.ELEMENT || arg[ELEMENT_KEY])) || arg)
239+
const script = Function(body.script)
240+
const args = transformPropertyWithMockFunction(body.args.map((arg: any) => (arg && (arg.ELEMENT || arg[ELEMENT_KEY])) || arg))
212241

213242
let result: any = null
214-
if (params.json.script.includes('resq')) {
215-
if (params.json.script.includes('react$$')) {
243+
if (body.script.includes('resq')) {
244+
if (body.script.includes('react$$')) {
216245
result = [
217246
{ [ELEMENT_KEY]: genericElementId },
218247
{ [ELEMENT_KEY]: 'some-elem-456' },
219248
{ [ELEMENT_KEY]: 'some-elem-789' },
220249
]
221-
} else if (params.json.script.includes('react$')) {
250+
} else if (body.script.includes('react$')) {
222251
result = args[0] === 'myNonExistingComp'
223252
? new Error('foobar')
224253
: { [ELEMENT_KEY]: genericElementId }
225254
} else {
226255
result = null
227256
}
228-
} else if (params.json.script.includes('testLocatorStrategy')) {
257+
} else if (body.script.includes('testLocatorStrategy')) {
229258
result = { [ELEMENT_KEY]: genericElementId }
230-
} else if (params.json.script.includes('testLocatorStrategiesMultiple')) {
259+
} else if (body.script.includes('testLocatorStrategiesMultiple')) {
231260
result = [
232261
{ [ELEMENT_KEY]: genericElementId },
233262
{ [ELEMENT_KEY]: 'some-elem-456' },
234263
{ [ELEMENT_KEY]: 'some-elem-789' },
235264
]
236-
} else if (params.json.script.includes('previousElementSibling')) {
237-
result = params.json.args[0][ELEMENT_KEY] === genericSubElementId
265+
} else if (body.script.includes('previousElementSibling')) {
266+
result = body.args[0][ELEMENT_KEY] === genericSubElementId
238267
? { [ELEMENT_KEY]: 'some-previous-elem' }
239268
: {}
240-
} else if (params.json.script.includes('parentElement')) {
241-
result = params.json.args[0][ELEMENT_KEY] === genericSubElementId
269+
} else if (body.script.includes('parentElement')) {
270+
result = body.args[0][ELEMENT_KEY] === genericSubElementId
242271
? { [ELEMENT_KEY]: 'some-parent-elem' }
243272
: {}
244-
} else if (params.json.script.includes('nextElementSibling')) {
245-
result = params.json.args[0][ELEMENT_KEY] === genericElementId
273+
} else if (body.script.includes('nextElementSibling')) {
274+
result = body.args[0][ELEMENT_KEY] === genericElementId
246275
? { [ELEMENT_KEY]: 'some-next-elem' }
247276
: {}
248-
} else if (params.json.script.includes('scrollX')) {
277+
} else if (body.script.includes('scrollX')) {
249278
result = [0, 0]
250-
} else if (params.json.script.includes('function isFocused')) {
279+
} else if (body.script.includes('function isFocused')) {
251280
result = true
252281
} else {
253282
result = script.apply(this, args)
@@ -257,9 +286,9 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
257286
value = Boolean(result) || result === false || result === 0 || result === null ? result : {}
258287
break
259288
} case `/session/${sessionId}/execute/async`: {
260-
const script = Function(params.json.script)
289+
const script = Function(body.script)
261290
let result
262-
script.call(this, ...params.json.args, (_result: any) => result = _result)
291+
script.call(this, ...body.args, (_result: any) => result = _result)
263292
value = result ?? {}
264293
break
265294
} case `${path}/${sessionId}/element/${genericElementId}/elements`:
@@ -351,10 +380,9 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
351380

352381
if (requestMock.retryCnt > 1) {
353382
const response = { value: null }
354-
return Promise.resolve({
355-
headers: { foo: 'bar' },
356-
statusCode: 200,
357-
body: response
383+
return Response.json(response, {
384+
status: 200,
385+
headers: { foo: 'bar' }
358386
})
359387
}
360388

@@ -366,31 +394,29 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
366394
}
367395
}
368396

369-
return Promise.resolve({
370-
headers: { foo: 'bar' },
371-
statusCode: 404,
372-
body: error
397+
return Response.json(error, {
398+
status: 404,
399+
headers: { foo: 'bar' }
373400
})
374401
}
375402

376403
/**
377404
* empty response
378405
*/
379406
if (uri.pathname === '/empty') {
380-
return Promise.resolve({
381-
headers: { foo: 'bar' },
382-
statusCode: 500,
383-
body: ''
407+
return Response.json('', {
408+
status: 500,
409+
headers: { foo: 'bar' }
384410
})
385411
}
386412

387413
/**
388414
* session error due to wrong path
389415
*/
390416
if (uri.pathname === '/wrong/path') {
391-
return Promise.resolve({
392-
headers: { foo: 'bar' },
393-
statusCode: 404
417+
return Response.json({}, {
418+
status: 404,
419+
headers: { foo: 'bar' }
394420
})
395421
}
396422

@@ -405,17 +431,16 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
405431
*/
406432
if (requestMock.retryCnt > 3) {
407433
const response = { value: 'caught' }
408-
return Promise.resolve({
409-
headers: { foo: 'bar' },
410-
statusCode: 200,
411-
body: response
434+
435+
return Response.json(response, {
436+
status: 200,
437+
headers: { foo: 'bar' }
412438
})
413439
}
414440

415-
return Promise.resolve({
416-
headers: { foo: 'bar' },
417-
statusCode: 400,
418-
body: {}
441+
return Response.json({}, {
442+
status: 400,
443+
headers: { foo: 'bar' }
419444
})
420445
}
421446

@@ -447,15 +472,12 @@ const requestMock: any = vi.fn().mockImplementation((uri, params) => {
447472
response = response.value
448473
}
449474

450-
return Promise.resolve({
451-
headers: { foo: 'bar' },
452-
statusCode,
453-
body: response
475+
return Response.json(response, {
476+
status: statusCode,
477+
headers: { foo: 'bar' }
454478
})
455479
})
456480

457-
requestMock.extend = vi.fn().mockReturnValue(requestMock)
458-
requestMock.put = vi.fn().mockReturnValue(Promise.resolve({}))
459481
requestMock.retryCnt = 0
460482
requestMock.setMockResponse = (value: any) => {
461483
manualMockResponse = value
@@ -472,4 +494,4 @@ requestMock.resetSessionId = () => {
472494
sessionId = defaultSessionId
473495
}
474496

475-
export default requestMock
497+
vi.stubGlobal('fetch', requestMock)

__mocks__/ky.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)