Skip to content

Commit

Permalink
Fix fetch call assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Aug 25, 2023
1 parent d7c252b commit be6fbaa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions plugin-server/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { URL } from 'url'

import { runInSpan } from '../sentry'

export async function trackedFetch(url: RequestInfo, init: RequestInit | undefined): Promise<Response> {
export async function trackedFetch(url: RequestInfo, init?: RequestInit): Promise<Response> {
const request = new Request(url, init)
return await runInSpan(
{
Expand All @@ -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<Response> {
export async function safeTrackedFetch(url: RequestInfo, init?: RequestInit): Promise<Response> {
const request = new Request(url, init)
return await runInSpan(
{
Expand Down
6 changes: 3 additions & 3 deletions plugin-server/tests/worker/vm.extra-lazy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
30 changes: 18 additions & 12 deletions plugin-server/tests/worker/vm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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)
})
})

Expand Down Expand Up @@ -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] })
})
Expand All @@ -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] })
})
Expand All @@ -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] })
})
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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
)
})
})

Expand Down

0 comments on commit be6fbaa

Please sign in to comment.