You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The global `fetch()` function is available in many JavaScript runtimes, including server-side runtimes and web browsers. While `fetch()` works similarly in both environments, there are some important differences to take into account when testing code that uses `fetch()` in a browser environment.
9
+
10
+
## Relative URLs with `fetch()`
11
+
12
+
Perhaps the most significant difference between using `fetch()` in a browser as opposed to a server environment is how relative URLs are handled. When you use `fetch()` in a browser, relative URLs are resolved relative to the current page's URL. This means that if you call `fetch("/api/data")` from a page at `https://example.com/page`, the request will be made to `https://example.com/api/data`. This happens automatically in the browser whenever you use `fetch()`, ensuring that requests go to the correct server.
13
+
14
+
If you try to use a relative URL with `fetch()` in a server-side environment, you'll get an error. This is because the server doesn't know what the base URL should be, so it can't resolve the relative URL. That means the same `fetch()` call that works in the browser won't work in a server environment and it's important to keep that in mind when writing tests.
15
+
16
+
## Mocking Browser Requests with Mentoss
17
+
18
+
Mentoss provides a way to mock browser requests in your tests, allowing you to test code that uses `fetch()` without making actual network requests. To mock a browser request, you can provide a `baseUrl` option to the `FetchMocker` constructor. This option specifies the base URL that relative URLs should be resolved against. You can then call a mocked `fetch()` using a relative URL. Here's an example:
19
+
20
+
```js {10, 26-27}
21
+
import { MockServer, FetchMocker } from"mentoss";
22
+
import { expect } from"chai";
23
+
24
+
constBASE_URL="https://api.example.com";
25
+
26
+
describe("My API", () => {
27
+
constserver=newMockServer(BASE_URL);
28
+
constmocker=newFetchMocker({
29
+
servers: [server],
30
+
baseUrl:BASE_URL
31
+
});
32
+
33
+
// extract the fetch function
34
+
constmyFetch=mocker.fetch;
35
+
36
+
// reset the server after each test
37
+
afterEach(() => {
38
+
server.clear();
39
+
});
40
+
41
+
it("should return a 200 status code", async () => {
42
+
43
+
// set up the route to test
44
+
server.get("/ping", 200);
45
+
46
+
// make the request
47
+
constresponse=awaitmyFetch("/ping");
48
+
49
+
// check the response
50
+
expect(response.status).to.equal(200);
51
+
});
52
+
});
53
+
```
54
+
55
+
In this example, the `baseUrl` option is set to `"https://api.example.com"`, which means that relative URLs will be resolved against that base URL. The test then calls `myFetch("/ping")`, which resolves to `"https://api.example.com/ping"` and makes a request to the server. This allows you to test code that uses relative URLs with `fetch()` in a browser-like environment.
56
+
57
+
<Asidetype="caution">
58
+
When using the `baseUrl` option with `FetchMocker`, make sure to set it to the correct base URL for your mock server. If you don't have a mock server defined for the base URL, the request will fail.
0 commit comments