Skip to content
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

Closed
ptim opened this issue Jun 1, 2022 · 2 comments
Closed

FR: mocking for request.tracer.fetch #27

ptim opened this issue Jun 1, 2022 · 2 comments

Comments

@ptim
Copy link

ptim commented Jun 1, 2022

Is it possible to mock request.tracer.fetch?

Related miniflare issue:

Currently, I think the best option for jest-environment-miniflare is to use Jest's function mocks to mock the global fetch function.
cloudflare/miniflare#162 (comment)

@ptim
Copy link
Author

ptim commented Oct 20, 2022

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 getMiniflareFetchMock.

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.

@ptim
Copy link
Author

ptim commented Oct 20, 2022

ah.. I think global.fetch might have been provided by my previous workaround: https://github.com/jefflau/jest-fetch-mock

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 createTestContext in the context of a Sunder project:

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
}

@ptim ptim closed this as completed Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant