fetch-light-my-request
Version:
fetch-mock wrapper over light-my-request
44 lines (32 loc) • 1.45 kB
Markdown
[](https://github.com/segevfiner/fetch-light-my-request/actions/workflows/ci.yml)
[](https://segevfiner.github.io/fetch-light-my-request/)
[] wrapper over [light-my-request]. This is useful to attach `fetch` based HTTP clients to
a tested plain HTTP or Fastify server.
[](https://segevfiner.github.io/fetch-light-my-request/)
[]: https://www.wheresrhys.co.uk/fetch-mock/
[]: https://www.npmjs.com/package/light-my-request
```ts
const fetchMock = createFetchMockLightMyRequest((req, res) => {
res.end("Hello, World!");
});
const res = await fetchMock.fetchHandler("http://localhost/hello");
console.log(res.text());
```
Or with [Fastify]:
```ts
import Fastify from "fastify";
const fastify = Fastify();
fastify.get("/hello", () => {
return "Hello, World!";
});
const fetchMock = createFetchMockLightMyRequestFromFastify(fastify);
const res = await fetchMock.fetchHandler("http://localhost/hello");
console.log(res.text());
```
You can also use `createFetchLightMyRequest` or `createFetchLightMyRequestFromFastify` to directly
return the resulting mock `fetch` function instead of the `FetchMock` instance.
[]: https://fastify.dev/
MIT.