2021-03-26 03:22:18 +01:00
|
|
|
const { readFileSync } = require('fs')
|
|
|
|
const { DateTime } = require('luxon')
|
|
|
|
const { join } = require('path')
|
|
|
|
|
2022-08-03 15:12:56 +02:00
|
|
|
import fetch from 'node-fetch'
|
|
|
|
|
2022-10-10 16:40:43 +02:00
|
|
|
import { status } from './src/utils/status'
|
|
|
|
|
2022-08-03 15:12:56 +02:00
|
|
|
jest.mock('node-fetch')
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-11-30 16:57:02 +01:00
|
|
|
const responsesToUrls = {
|
|
|
|
'https://google.com/results.json?query=fetched': { count: 2, query: 'bla', results: [true, true] },
|
2022-04-01 16:04:09 +02:00
|
|
|
'https://mmdbcdn.posthog.net/': readFileSync(join(__dirname, 'tests', 'assets', 'GeoLite2-City-Test.mmdb.br')),
|
2021-08-17 18:36:12 +02:00
|
|
|
'https://app.posthog.com/api/event?token=THIS+IS+NOT+A+TOKEN+FOR+TEAM+2': { hello: 'world' },
|
2021-03-26 03:22:18 +01:00
|
|
|
}
|
|
|
|
const headersToUrls = {
|
2022-04-01 16:04:09 +02:00
|
|
|
'https://mmdbcdn.posthog.net/': new Map([
|
2021-03-26 03:22:18 +01:00
|
|
|
['content-type', 'vnd.maxmind.maxmind-db'],
|
|
|
|
['content-disposition', `attachment; filename="GeoLite2-City-${DateTime.local().toISODate()}.mmdb"`],
|
|
|
|
]),
|
2020-11-30 16:57:02 +01:00
|
|
|
}
|
2022-08-03 15:12:56 +02:00
|
|
|
|
|
|
|
fetch.mockImplementation(
|
2021-08-17 18:36:12 +02:00
|
|
|
(url, options) =>
|
2020-11-30 16:57:02 +01:00
|
|
|
new Promise((resolve) =>
|
|
|
|
resolve({
|
2021-03-26 03:22:18 +01:00
|
|
|
buffer: () => new Promise((resolve) => resolve(responsesToUrls[url]) || Buffer.from('fetchmock')),
|
2020-12-07 16:07:44 +01:00
|
|
|
json: () => new Promise((resolve) => resolve(responsesToUrls[url]) || { fetch: 'mock' }),
|
|
|
|
text: () => new Promise((resolve) => resolve(JSON.stringify(responsesToUrls[url])) || 'fetchmock'),
|
2021-08-17 18:36:12 +02:00
|
|
|
status: () => (options.method === 'PUT' ? 201 : 200),
|
2021-03-26 03:22:18 +01:00
|
|
|
headers: headersToUrls[url],
|
2020-11-30 16:57:02 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
2022-10-10 16:40:43 +02:00
|
|
|
|
|
|
|
// NOTE: in testing we use the pino-pretty transport, which results in a handle
|
|
|
|
// that we need to close to allow Jest to exit properly.
|
|
|
|
afterAll(() => status.close())
|