Skip to content

Commit e29a21a

Browse files
sileskyKrishnamurti Subramanian
andauthored
Fix webdriver.io interception issue (#742) by choosing fetch function at call time rather than import time (#742)
Co-authored-by: Krishnamurti Subramanian <krishnamurti_subramanian@@intuit.com>
1 parent 88c91c8 commit e29a21a

File tree

7 files changed

+54
-14
lines changed

7 files changed

+54
-14
lines changed

.changeset/quiet-foxes-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Fix webdriver.io interception bug. Refactor to use native fetch where unfetch is unavailable.

packages/browser/src/browser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getProcessEnv } from '../lib/get-process-env'
22
import { getCDN, setGlobalCDNUrl } from '../lib/parse-cdn'
33

4-
import fetch from 'unfetch'
4+
import { fetch } from '../lib/fetch'
55
import { Analytics, AnalyticsSettings, InitOptions } from '../core/analytics'
66
import { Context } from '../core/context'
77
import { Plan } from '../core/events'

packages/browser/src/core/stats/remote-metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'unfetch'
1+
import { fetch } from '../../lib/fetch'
22
import { version } from '../../generated/version'
33
import { getVersionType } from '../../plugins/segmentio/normalize'
44

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { fetch } from '../fetch'
2+
import { getGlobal } from '../get-global'
3+
import unfetch from 'unfetch'
4+
5+
jest.mock('unfetch')
6+
const unfetchMock = jest.mocked(unfetch).mockResolvedValue({} as Response)
7+
8+
jest.mock('../get-global')
9+
const getGlobalMock = jest.mocked(getGlobal)
10+
11+
describe(fetch, () => {
12+
const testFetchArgs = ['http://foo.com', {}] as const
13+
14+
it('should call native fetch if available', () => {
15+
const nativeFetchMock = jest.fn()
16+
getGlobalMock.mockReturnValue({ ...window, fetch: nativeFetchMock })
17+
void fetch(...testFetchArgs)
18+
expect(nativeFetchMock).toBeCalledWith(...testFetchArgs)
19+
expect(unfetchMock).not.toBeCalled()
20+
})
21+
it('should fall back to unfetch in non-browserlike environment', () => {
22+
getGlobalMock.mockReturnValue(null)
23+
void fetch(...testFetchArgs)
24+
expect(unfetchMock).toBeCalledWith(...testFetchArgs)
25+
})
26+
it('should fall back to unfetch if native fetch is unsupported', () => {
27+
getGlobalMock.mockReturnValue({
28+
...window,
29+
fetch: undefined,
30+
} as any)
31+
32+
void fetch(...testFetchArgs)
33+
expect(unfetchMock).toBeCalledWith(...testFetchArgs)
34+
})
35+
})

packages/browser/src/lib/fetch.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unfetch from 'unfetch'
2+
import { getGlobal } from './get-global'
3+
4+
/**
5+
* Wrapper around native `fetch` containing `unfetch` fallback.
6+
*/
7+
export const fetch: typeof global.fetch = (...args) => {
8+
const global = getGlobal()
9+
return ((global && global.fetch) || unfetch)(...args)
10+
}

packages/browser/src/plugins/segmentio/batched-dispatcher.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import unfetch from 'unfetch'
21
import { SegmentEvent } from '../../core/events'
2+
import { fetch } from '../../lib/fetch'
33
import { onPageLeave } from '../../lib/on-page-leave'
44

5-
let fetch = unfetch
6-
if (typeof window !== 'undefined') {
7-
fetch = window.fetch || unfetch
8-
}
9-
105
type BatchingConfig = {
116
size?: number
127
timeout?: number

packages/browser/src/plugins/segmentio/fetch-dispatcher.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import unfetch from 'unfetch'
2-
3-
let fetch = unfetch
4-
if (typeof window !== 'undefined') {
5-
fetch = window.fetch || unfetch
6-
}
1+
import { fetch } from '../../lib/fetch'
72

83
export type Dispatcher = (url: string, body: object) => Promise<unknown>
94

0 commit comments

Comments
 (0)