Skip to content

Commit c71311c

Browse files
authored
fall back to node-fetch only if native fetch is unsupported (#679)
1 parent 8b2ff4a commit c71311c

File tree

6 files changed

+11
-5
lines changed

6 files changed

+11
-5
lines changed

packages/node/src/__tests__/graceful-shutdown-integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createSuccess } from './test-helpers/factories'
22

33
const fetcher = jest.fn()
4-
jest.mock('node-fetch', () => fetcher)
4+
jest.mock('../lib/fetch', () => fetcher)
55

66
import { Analytics, SegmentEvent } from '../app/analytics-node'
77
import { sleep } from './test-helpers/sleep'

packages/node/src/__tests__/http-integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fetcher = jest.fn()
2-
jest.mock('node-fetch', () => fetcher)
2+
jest.mock('../lib/fetch', () => fetcher)
33

44
import { createSuccess } from './test-helpers/factories'
55
import { Analytics } from '..'

packages/node/src/__tests__/integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fetcher = jest.fn()
2-
jest.mock('node-fetch', () => fetcher)
2+
jest.mock('../lib/fetch', () => fetcher)
33

44
import { Analytics } from '../index'
55
import { CorePlugin as Plugin } from '@segment/analytics-core'

packages/node/src/lib/fetch.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { default as nodeFetch } from 'node-fetch'
2+
3+
// made this a default export so it mocks the same as node-fetch in tests.
4+
const fetch = globalThis.fetch || nodeFetch
5+
6+
export default fetch

packages/node/src/plugins/segmentio/__tests__/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fetcher = jest.fn()
2-
jest.mock('node-fetch', () => fetcher)
2+
jest.mock('../../../lib/fetch', () => fetcher)
33

44
import { CoreContext } from '@segment/analytics-core'
55
import { createNodeEventFactory } from '../../../lib/create-node-event-factory'

packages/node/src/plugins/segmentio/publisher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { backoff, CoreContext } from '@segment/analytics-core'
2-
import fetch from 'node-fetch'
32
import { tryCreateFormattedUrl } from '../../lib/create-url'
43
import { extractPromiseParts } from '../../lib/extract-promise-parts'
4+
import fetch from '../../lib/fetch'
55
import { ContextBatch } from './context-batch'
66

77
function sleep(timeoutInMs: number): Promise<void> {

0 commit comments

Comments
 (0)