# @nodifier/http-mock

> A node module to mock http/https requests made for unit testing etc.

Latest version **0.0.7** (published 2023-08-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install @nodifier/http-mock
pnpm add @nodifier/http-mock
yarn add @nodifier/http-mock
bun add @nodifier/http-mock
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2023-08-07 |
| First published | 2023-08-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 131.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | gamemasterdev |
| Maintainers | gamemasterdev |
| Keywords | nodify, http-mock, http, https, jest, nodejs |

## Links

- npm: https://www.npmjs.com/package/@nodifier/http-mock
- npm.io page: https://npm.io/package/@nodifier/http-mock

## Dependencies (1)

- [http-request-mock](https://npm.io/package/http-request-mock.md) ^1.8.17

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [fb-rosters](https://npm.io/package/fb-rosters.md) — 0 weekly downloads
- [linked-faker](https://npm.io/package/linked-faker.md) — 0 weekly downloads
- [mock-chronik-client](https://npm.io/package/mock-chronik-client.md) — 0 weekly downloads

## Recent versions

- 0.0.7 (latest) — 2023-08-07
- 0.0.6 — 2023-08-07
- 0.0.5 — 2023-08-07
- 0.0.4 — 2023-08-07
- 0.0.3 — 2023-08-04
- 0.0.2 — 2023-08-04
- 0.0.1 — 2023-08-03

## README

# @nodifier/http-mock

A node module to mock http/https requests made for unit testing etc. 
Suitable for unit testing framework like Jest.

## How to use?

```typescript

import { justMock } from '@nodify/http-mock';

describe('some unit tests', () => {
    it('should mock the request', async () => {
        const jm = justMock('http://example.com/api').get(200).response('hello world')

        // Your node request function that you wish to mock the request
        const output = await makeRequest();

        jm.done();

        expect(output).toStrictEqual('hello world');
    })
})

```

### Specify the endpoint to mock

```typescript

// The endpoint to mock
jestMock('http://example.com/api')

```

### Mocking the response status code

You can mock the response from any of the request methods available. [view](#supported-request-methods)

```typescript

// response status code to mock as a response
get(200)

```

### Mocking the response body

The response body can be mock by using the `response` method as follows:

```typescript

// Will return "hello world" as the request response
response('hello world');

```

### Ending mock

The mock always have to be ended with `done` method when the unit test is done like follows...

```typescript

const jm = justMock('http://example.com/api').get(200).response('hello world')

const output = await makeRequest();

// Ends the mock
jm.done();

expect(output).toBeTruthy();

```

## Supported request methods

1. [GET](#get)
2. [POST](#post)
3. [PUT](#put)
4. [DELETE](#delete)

### GET

```typescript

justMock('http://example.com/api').get(200)

```

### POST

```typescript

justMock('http://example.com/api').post(200)

```

### PUT

```typescript

justMock('http://example.com/api').put(200)

```

### DELETE

```typescript

justMock('http://example.com/api').del(200)

```

---
_Source: https://npm.io/package/@nodifier/http-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
