-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FR: mocking for request.tracer.fetch
#27
Comments
Hey Erwin! Circling back to this (had forgotten I raised it!), as I had a workaround, but it doesn't work since updating to latest miniflare, which provides import { Span } from '@cloudflare/workers-honeycomb-logger/dist/logging'
export const honeycombRequestTracer = {
addData: () => {},
log: () => {},
fetch: global.fetch, // ❌ now undefined...
} as unknown as Span
export const createHoneycombRequest = (request: Request, fetch?: any) => {
request.tracer = honeycombRequestTracer
// ❌ (getMinflareFetchMock returns a MockAgent, not fetch)
if (fetch) request.tracer.fetch = fetch
// ❌ and global.fetch is now undefined for some reason
if (typeof request.tracer.fetch !== 'function') throw new Error('fetch is not a function!')
return request
} This is a bit of an FYI, as I plan to remove Honeycomb (which is ace!) to simplify. |
ah.. I think The following seems to be working: import { Span } from '@cloudflare/workers-honeycomb-logger/dist/logging'
export const honeycombRequestTracer = {
addData: () => {},
log: () => {},
} as unknown as Span
export const createHoneycombRequest = (
request: Request,
_fetch?: () => Promise<Response>,
) => {
request.tracer = honeycombRequestTracer
request.tracer.fetch = _fetch ?? fetch
return request
} Posting as a recipe / as invitation for feedback 😊 It's used in my helper type CreateTestContextOptions = {
request?: Request
env?: Env
fetch?: () => Promise<Response>
}
/**
* Helper that returns a `ctx` object that satisfies middlewares.
*
* Optionally takes a Request or Env, useful when you need to access bindings
*
``ts
const ctx = createTestContext()
const ctx = createTestContext({ request: new Request('http://localhost/') })
const env = createTestEnv()
const ctx = createTestContext({ env })
const ctx = createTestContext({ env: { MODE: 'production' } as Env })
``
*
* @see https://github.com/SunderJS/sunder/blob/master/test/helpers.ts#L9
*/
export function createTestContext(
options: CreateTestContextOptions = {},
): MyContext {
const event = createFetchEvent(
options.request || new Request('http://localhost/'),
)
const env = options.env || createTestEnv()
const ctx = new Context({
event: event,
env,
request: createHoneycombRequest(event.request, options.fetch),
}) as MyContext
ctx.data.sentry = createSentryMock(ctx)
return ctx
} |
Is it possible to mock
request.tracer.fetch
?Related miniflare issue:
The text was updated successfully, but these errors were encountered: