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

fetch-mock 12.0 + node:test issues #863

Closed
danehansen opened this issue Oct 30, 2024 · 2 comments
Closed

fetch-mock 12.0 + node:test issues #863

danehansen opened this issue Oct 30, 2024 · 2 comments

Comments

@danehansen
Copy link

node v22.5.1
npm 10.8.2
fetch-mock 12.0.2

i was using fetch-mock v11 which was working great along with node:test, but i upgraded to v12 and after taking steps to migrate, my preexisting tests will now pass as long as i run one at a time via --test-only but will fail if i attempt to run any combination of multiple tests. any ideas what i am missing? am i leaving out some awaits or something? heres a copy of my test file. thanks.

import { strict as assert } from "node:assert";
import { describe, it } from "node:test";
import fetchMock from "fetch-mock";
import { FETCH_TIMEOUT_MS } from "../errors/TimeoutError";
import { fetcher, fetcherResult, fetcherTimeout } from "./core";

describe("core.ts", async () => {
  const FAKE_URL = "https://www.fake.com";

  describe("fetcher()", async () => {
    it("adds common properties to body", async () => {
      fetchMock.mockGlobal().route(FAKE_URL, 200);
      await fetcher(FAKE_URL, { body: {} });
      const lastCallArg = fetchMock.callHistory.lastCall()?.args[1] as {
        method: string;
        body: string;
      };
      const method = lastCallArg.method;
      const body = JSON.parse(lastCallArg.body);
      const ver = body.ver;
      assert.equal(method, "POST");
      assert.equal(ver, 1);
      fetchMock.unmockGlobal();
    });
  });

  describe("fetcherTimeout()", async () => {
    it("allows a quick endpoint to funciton unimpeded", async () => {
      const options = { body: { rand: Math.random() } };
      fetchMock
        .mockGlobal()
        .route(FAKE_URL, options, { delay: FETCH_TIMEOUT_MS * 0.1 });
      const response = await fetcherTimeout<Response>(FAKE_URL, options);
      const {
        body: { rand },
      } = (await fetcherResult<Response>(options, response)) as any;
      assert.equal(rand, options.body.rand);
      fetchMock.unmockGlobal();
    });
  });

  describe("fetcherResult()", async () => {
    it("returns JSON object", async () => {
      const options = { body: { foo: "bar" } };
      fetchMock.mockGlobal().route(FAKE_URL, { options });
      const response = await fetcher(FAKE_URL, options);
      const json = (await fetcherResult(options, response as Response)) as any;
      assert.deepEqual(json.options, options);
      fetchMock.unmockGlobal();
    });
  });
});
@wheresrhys
Copy link
Owner

It makes sense that they'd only work one at a time as each one mocks and adds different routes to fetch independently; if they run in parallel then they are bound to interfere as they are all acting on a single fetch implementation.

Are you able to share your code from before the upgrade? I'm confused how they could have passed TBH

@wheresrhys
Copy link
Owner

Closing due to inactivity

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

2 participants