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

[Miniflare] Cloudflare Pages Support for unit testing #4365

Closed
uhthomas opened this issue Aug 21, 2023 · 2 comments
Closed

[Miniflare] Cloudflare Pages Support for unit testing #4365

uhthomas opened this issue Aug 21, 2023 · 2 comments
Labels
enhancement New feature or request miniflare Relating to Miniflare

Comments

@uhthomas
Copy link
Member

I saw cloudflare/miniflare#113, but I am interested in using miniflare with Jest to test Cloudflare Pages Functions.

It doesn't seem possible to create a mock EventContext with Miniflare at current.

@mrbbot
Copy link
Contributor

mrbbot commented Nov 7, 2023

Hey! 👋 Thanks for raising this issue, and apologies for the delayed response. I'm going to transfer this to workers-sdk as that's the new home for Miniflare 3, and that will make it easier for us to track this. We're planning to revamp Miniflare's unit testing environments for Miniflare 3 soon, and will consider Pages Functions support when we're implementing this. For now, since EventContext is just a plain-object (https://workers-types.pages.dev/#EventContext), you should be able to create a mock context with something like...

const ctx = new ExecutionContext();
const context: EventContext<Env, string, Record<string, unknown>> = {
  request: new Request("http://localhost"),
  functionPath: "",
  waitUntil: ctx.waitUntil.bind(ctx),
  passThroughOnException: ctx.passThroughOnException.bind(ctx),
  async next(input) {
    return new Response();
  },
  env: {
    ...getMiniflareBindings(),
    ASSETS: {
      async fetch(input, info) {
        const request = new Request(input, info);
        const url = new URL(request.url);
        if (url.pathname === "/index.html") {
          return new Response("<h1>Hello!</h1>");
        } else {
          return new Response(null, { status: 404 });
        }
      }
    }
  },
  params: {},
  data: {}
};

// ...do something with `context`...

await getMiniflareWaitUntil(ctx);

@mrbbot mrbbot changed the title Cloudflare Pages Support [Miniflare] Cloudflare Pages Support for unit testing Nov 7, 2023
@mrbbot mrbbot transferred this issue from cloudflare/miniflare Nov 7, 2023
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Nov 7, 2023
@mrbbot mrbbot added enhancement New feature or request miniflare Relating to Miniflare labels Nov 7, 2023
@mrbbot mrbbot moved this from Untriaged to Backlog in workers-sdk Nov 7, 2023
@penalosa
Copy link
Contributor

penalosa commented Nov 8, 2024

Since this issue was opened, we've released the revamped Vitest work for Miniflare 3, which should work with Pages Functions: https://developers.cloudflare.com/workers/testing/unit-testing/

@penalosa penalosa closed this as completed Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request miniflare Relating to Miniflare
Projects
None yet
Development

No branches or pull requests

3 participants