From a030f1f3fa20616648029e251c3e04b2a1d8ba2f Mon Sep 17 00:00:00 2001 From: Michael Matloka Date: Fri, 25 Aug 2023 15:07:06 +0200 Subject: [PATCH] Fix fetch call assertions --- plugin-server/src/utils/fetch.ts | 4 +-- .../tests/worker/vm.extra-lazy.test.ts | 6 ++-- plugin-server/tests/worker/vm.test.ts | 30 +++++++++++-------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/plugin-server/src/utils/fetch.ts b/plugin-server/src/utils/fetch.ts index 309a4c04d5cda..298e9e70debad 100644 --- a/plugin-server/src/utils/fetch.ts +++ b/plugin-server/src/utils/fetch.ts @@ -8,7 +8,7 @@ import { URL } from 'url' import { runInSpan } from '../sentry' -export async function trackedFetch(url: RequestInfo, init: RequestInit | undefined): Promise { +export async function trackedFetch(url: RequestInfo, init?: RequestInit): Promise { const request = new Request(url, init) return await runInSpan( { @@ -22,7 +22,7 @@ export async function trackedFetch(url: RequestInfo, init: RequestInit | undefin trackedFetch.isRedirect = fetch.isRedirect trackedFetch.FetchError = FetchError -export async function safeTrackedFetch(url: RequestInfo, init: RequestInit | undefined): Promise { +export async function safeTrackedFetch(url: RequestInfo, init?: RequestInit): Promise { const request = new Request(url, init) return await runInSpan( { diff --git a/plugin-server/tests/worker/vm.extra-lazy.test.ts b/plugin-server/tests/worker/vm.extra-lazy.test.ts index 3c66d319da44d..e571b2f809b59 100644 --- a/plugin-server/tests/worker/vm.extra-lazy.test.ts +++ b/plugin-server/tests/worker/vm.extra-lazy.test.ts @@ -39,7 +39,7 @@ describe('VMs are extra lazy 💤', () => { expect(lazyVm.ready).toEqual(true) expect(lazyVm.setupPluginIfNeeded).not.toHaveBeenCalled() - expect(fetch).toHaveBeenCalledWith('https://onevent.com/') + expect(fetch).toHaveBeenCalledWith('https://onevent.com/', undefined) }) test('VM with jobs gets setup immediately', async () => { @@ -64,7 +64,7 @@ describe('VMs are extra lazy 💤', () => { expect(lazyVm.ready).toEqual(true) expect(lazyVm.setupPluginIfNeeded).not.toHaveBeenCalled() - expect(fetch).toHaveBeenCalledWith('https://onevent.com/') + expect(fetch).toHaveBeenCalledWith('https://onevent.com/', undefined) }) test('VM without tasks delays setup until necessary', async () => { @@ -91,7 +91,7 @@ describe('VMs are extra lazy 💤', () => { await lazyVm.getOnEvent() expect(lazyVm.ready).toEqual(true) expect(lazyVm.setupPluginIfNeeded).toHaveBeenCalled() - expect(fetch).toHaveBeenCalledWith('https://onevent.com/') + expect(fetch).toHaveBeenCalledWith('https://onevent.com/', undefined) }) test('getting methods and tasks returns null if plugin is in errored state', async () => { diff --git a/plugin-server/tests/worker/vm.test.ts b/plugin-server/tests/worker/vm.test.ts index 5351d9eb6b55a..138c813d5c70b 100644 --- a/plugin-server/tests/worker/vm.test.ts +++ b/plugin-server/tests/worker/vm.test.ts @@ -122,7 +122,7 @@ describe('vm tests', () => { }) expect(fetch).not.toHaveBeenCalled() await vm.methods.teardownPlugin!() - expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=hoho') + expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=hoho', undefined) }) test('processEvent', async () => { @@ -376,7 +376,7 @@ describe('vm tests', () => { event: 'export', } await vm.methods.onEvent!(event) - expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=export') + expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=export', undefined) }) test('export default', async () => { @@ -395,7 +395,7 @@ describe('vm tests', () => { event: 'default export', } await vm.methods.onEvent!(event) - expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=default export') + expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=default export', undefined) }) }) @@ -723,7 +723,7 @@ describe('vm tests', () => { } await vm.methods.processEvent!(event) - expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched') + expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched', undefined) expect(event.properties).toEqual({ count: 2, query: 'bla', results: [true, true] }) }) @@ -745,7 +745,7 @@ describe('vm tests', () => { } await vm.methods.processEvent!(event) - expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched') + expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched', undefined) expect(event.properties).toEqual({ count: 2, query: 'bla', results: [true, true] }) }) @@ -766,7 +766,7 @@ describe('vm tests', () => { } await vm.methods.processEvent!(event) - expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched') + expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched', undefined) expect(event.properties).toEqual({ count: 2, query: 'bla', results: [true, true] }) }) @@ -1051,7 +1051,7 @@ describe('vm tests', () => { event: 'onEvent', } await vm.methods.onEvent!(event) - expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=onEvent') + expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=onEvent', undefined) }) describe('exportEvents', () => { @@ -1085,7 +1085,7 @@ describe('vm tests', () => { await vm.methods.onEvent!({ ...defaultEvent, event: 'otherEvent2' }) await vm.methods.onEvent!({ ...defaultEvent, event: 'otherEvent3' }) await delay(1010) - expect(fetch).toHaveBeenCalledWith('https://export.com/results.json?query=otherEvent2&events=2') + expect(fetch).toHaveBeenCalledWith('https://export.com/results.json?query=otherEvent2&events=2', undefined) expect(hub.appMetrics.queueMetric).toHaveBeenCalledWith({ teamId: pluginConfig39.team_id, pluginConfigId: pluginConfig39.id, @@ -1136,8 +1136,8 @@ describe('vm tests', () => { await vm.methods.onEvent!(event) await delay(1010) expect(fetch).toHaveBeenCalledTimes(4) - expect(fetch).toHaveBeenCalledWith('https://onevent.com/') - expect(fetch).toHaveBeenCalledWith('https://export.com/results.json?query=exported&events=2') + expect(fetch).toHaveBeenCalledWith('https://onevent.com/', undefined) + expect(fetch).toHaveBeenCalledWith('https://export.com/results.json?query=exported&events=2', undefined) }) test('buffers bytes with exportEventsBufferBytes', async () => { @@ -1264,10 +1264,16 @@ describe('vm tests', () => { indexJs ) await vm.methods.onEvent!(defaultEvent) - expect(fetch).not.toHaveBeenCalledWith('https://export.com/results.json?query=default event&events=1') + expect(fetch).not.toHaveBeenCalledWith( + 'https://export.com/results.json?query=default event&events=1', + undefined + ) await vm.methods.teardownPlugin!() - expect(fetch).toHaveBeenCalledWith('https://export.com/results.json?query=default event&events=1') + expect(fetch).toHaveBeenCalledWith( + 'https://export.com/results.json?query=default event&events=1', + undefined + ) }) })