http mocking
- Dominant language
- TypeScript
- Stars
- 3.6k
- Forks
- 681
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
I would like to mock out http requests inside deno tests
**Describe the solution you'd like**
It would be awesome if deno could provide a clean interface for this in the std library. In nodejs land, there is a pretty killer library called [nock](https://www.npmjs.com/package/nock) that accomplishes this. In addition to mocking out calls, it assists in setting up assertions, request counts, and a whole slew of useful tools. I imagine in deno the interface would be fairly similar:
```ts
import { MockHttp } from 'https://deno.land/std@0.136.0/testing/mock_http.ts'
describe('my scraper library', () => {
const mock_http = MockHttp()
beforeEach(() => {
mock_http.start()
})
afterEach(() => {
mock_http.clean()
})
it(() => {
mock_http
.get('https://google.com')
.reply(new Response('', {status: 404}))
await myScraper('google.com')
})
})
```
**Describe alternatives you've considered**
I believe this can be implementing myself, simply by doing the following:
```ts
const fetchStub = stub(window, "fetch", returnsNext([new Response(...)]));
// do testing
fetchStub.restore();
```
but this feels like a common enough pattern/complex enough that there would be value in it existing in std
Contributor guide
Assessment
This issue has not been assessed yet.