Broadly there are two (not mutually exclusive) ways to unit test* a function like this:
Isolated test, with test doubles replacing the collaborators:
import httpFetch from "wherever";
import myFunction from "somewhere";
jest.mock("wherever");
describe("myFunction", () => {
it("calls httpFetch", async () => {
httpFetch.mockResolvedValue();
await myFunction({});
expect(httpFetch).toHaveBeenCalledWith(
"http://localhost/api/send",
{ method: "POST" },
{ "Content-Type": "application/json", Authorization: "Bearer 12ABC" }
);
});
});
This is the "easiest" way to do it, but now you're coupled to the httpFetch interface, which breaks the rule "don't mock what you don't own" - if that library's interface changes at some point, these tests won't tell you that.
Integration test, checking what happens at the transport layer using something like Nock:
import nock from "nock";
import myFunction from "somewhere";
describe("myFunction", async () => {
it("makes the right request", () => {
const scope = nock("http://localhost/api", {
reqheaders: {
"Content-Type": "application/json",
Authorization: "Bearer 12ABC",
},
})
.post("/send")
.reply(201);
await myFunction({});
scope.done();
});
});
This takes a bit more setup, but means you are less coupled to the httpFetch interface - you could upgrade that library or switch to a different one, for example, and still be confident things were working.
There are other ways to decouple from the specific library's interface; you could write a facade around it and mock that instead, for example. But you'd still want to know that the right request was being made, and you shouldn't test the facade against a test double of the library for the same reason as before.
You may also have higher-level tests, e.g. E2E tests against the real backend or contract tests against a stub of it; this will impact how you want to balance the number and type of your lower-level tests. Overall these options look something like:
System: [Service] -> [Library] -> [HTTP] -> [Backend]
Isolated: |<----->| -> (Test Double)
Integration: |<------------------>| -> (Nock)
Contract: |<---------------------------->| -> (Stub)
E2E: |<----------------------------------------->|
Remember that the goal (or one of them) is to be confident that the code you're writing works and that if that stops being the case you'll find out promptly in a way that helps you fix it.
* There are lots of ideas around exactly what might comprise a "unit test". Given the principles of speed, independence and parallelisability, the definition I've used in this context is: a test that doesn't actually make a network request.
httpFetch, so you could replace it with a test double (although note your function doesn't actually return anything...) and check it gets called with the right things (apparently not including the payload). Alternatively you can do more of an integration test using something likenockto check the right request gets made.myFunctionprobably shouldn't return 2xx or 5xx - that's a transport layer detail, you don't want your whole app relying on it.