-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
7 changed files
with
54 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-next': patch | ||
--- | ||
|
||
Fix webdriver.io interception bug. Refactor to use native fetch where unfetch is unavailable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { fetch } from '../fetch' | ||
import { getGlobal } from '../get-global' | ||
import unfetch from 'unfetch' | ||
|
||
jest.mock('unfetch') | ||
const unfetchMock = jest.mocked(unfetch).mockResolvedValue({} as Response) | ||
|
||
jest.mock('../get-global') | ||
const getGlobalMock = jest.mocked(getGlobal) | ||
|
||
describe(fetch, () => { | ||
const testFetchArgs = ['http://foo.com', {}] as const | ||
|
||
it('should call native fetch if available', () => { | ||
const nativeFetchMock = jest.fn() | ||
getGlobalMock.mockReturnValue({ ...window, fetch: nativeFetchMock }) | ||
void fetch(...testFetchArgs) | ||
expect(nativeFetchMock).toBeCalledWith(...testFetchArgs) | ||
expect(unfetchMock).not.toBeCalled() | ||
}) | ||
it('should fall back to unfetch in non-browserlike environment', () => { | ||
getGlobalMock.mockReturnValue(null) | ||
void fetch(...testFetchArgs) | ||
expect(unfetchMock).toBeCalledWith(...testFetchArgs) | ||
}) | ||
it('should fall back to unfetch if native fetch is unsupported', () => { | ||
getGlobalMock.mockReturnValue({ | ||
...window, | ||
fetch: undefined, | ||
} as any) | ||
|
||
void fetch(...testFetchArgs) | ||
expect(unfetchMock).toBeCalledWith(...testFetchArgs) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import unfetch from 'unfetch' | ||
import { getGlobal } from './get-global' | ||
|
||
/** | ||
* Wrapper around native `fetch` containing `unfetch` fallback. | ||
*/ | ||
export const fetch: typeof global.fetch = (...args) => { | ||
const global = getGlobal() | ||
return ((global && global.fetch) || unfetch)(...args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters