denoland / denoland/std

http mocking

Open
#2,156 0 comments 0 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.