-
Notifications
You must be signed in to change notification settings - Fork 203
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
How to mock internal fetch from end-to-end (e2e) test? #609
Comments
I'm having the same issue with I'm going to try removing the import { Miniflare } from "miniflare";
const mf = new Miniflare({...});
afterAll(() => {
mf.dispose();
}); to see if having miniflare in the same process as vitest (and the edit: based on #588 (comment) - it would appear the move to workerd in miniflare v3 did break fetch mocking. edit2: Looks like #632 added the |
I achieve this goal with the "miniflare" and "@miniflare/core" package in version 2.14.0. I suggest doing something similar to this: import { createFetchMock } from "@miniflare/core";
import getPort from "get-port";
// use join path
const MAIN_ENTRYPOINT_PATH = "./dist/index.js";
const WRANGLER_CONFIG_PATH = "./wrangler.toml";
const createWorker = async (fetchMock) => {
const options = {
host: "localhost",
port: await getPort(),
scriptPath: MAIN_ENTRYPOINT_PATH,
wranglerConfigPath: WRANGLER_CONFIG_PATH,
modules: true,
d1Persist: true,
d1Databases: ["__D1_BETA__DB"],
fetchMock,
};
const url = `http://${options.host}:${options.port}`;
const worker = new Miniflare(options);
await worker.startServer();
return {
url,
worker,
stop: async () => await worker.stop(),
};
};
let fetchMock: ReturnType<typeof createFetchMock>;
let server: any; // type according to your server
beforeAll(async () => {
fetchMock = createFetchMock();
server = await createWorker(fetchMock);
});
afterAll(async () => {
await server.stop();
});
test("worker", async () => {
const expectedUser = { id: 1, name: "John Doe" };
const origin = fetchMock.get("https://jsonplaceholder.typicode.com");
origin
.intercept({ method: "GET", path: "/users/1" })
.reply(200, expectedUser);
const response = await fetch(`${server.url}/users/1`);
const user = await response.json();
expect(user).toHaveProperty("name", expectedUser.name);
}); |
Hey! 👋 Apologies for not replying here, but it looks like you've got a solution. I'm going to close this now. 👍 |
@sofimiazzi solution doesn't work anymore with miniflare 3 (no export for createFetchMock), can we reopen @mrbbot ? The docs are missing for Testing under miniflare and the import { fetchMock } from "cloudflare:test";
import { beforeAll, afterEach, it, expect } from "vitest";
beforeAll(() => {
// Enable outbound request mocking...
fetchMock.activate();
// ...and throw errors if an outbound request isn't mocked
fetchMock.disableNetConnect();
});
// Ensure we matched every mock we defined
afterEach(() => fetchMock.assertNoPendingInterceptors());
it("mocks requests", async () => {
// Mock the first request to `https://example.com`
fetchMock
.get("https://example.com")
.intercept({ path: "/" })
.reply(200, "body");
const response = await fetch("https://example.com/");
expect(await response.text()).toBe("body");
}); Assuming example.com is the worker under test, why would you mock the worker you're trying to test? |
Hi, there! I am trying to write an end-to-end (e2e) test for my application, which involves making a request to an external API using the
fetch
function. However, I want to mock the internalfetch
call in order to control the response and ensure predictable testing. Currently, when I run the test, the response does not match the expected values. I would appreciate any assistance in understanding how to mock the internalfetch
call correctly in my e2e test.Repository with the complete example code cfw-mock (just install the packages and run
npm run test
).Here is the code snippet for the application:
And here is the code snippet for the e2e test:
In my e2e test, I have a specific scenario where I need to mock the internal
fetch
call to control the response and validate the behavior of my application. I have attempted to use thegetMiniflareFetchMock()
function to create a mock, and I intercept theGET
request to/users/1
and respond with the expected user object. However, when I run the test, the response does not match the expected values, and the assertion fails.I would appreciate any guidance or suggestions on how to properly mock the internal
fetch
call in my e2e test, so I can ensure predictable testing and validate the response data.The text was updated successfully, but these errors were encountered: