npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@reagent/axios-http-mock

v0.1.0

Published

Mock adapter for Axios HTTP requests

Downloads

4

Readme

Axios HTTP Mocking Library

Mock adapter for Axios HTTP client with a fluent type-safe interface.

Installation

Install from NPM:

$ yarn add @reagent/axios-http-mock

Usage

Creating an instance of HttpMock will expose an adapter to use when configuring your Axios client:

import Axios from 'axios';
import { HttpMock, HttpStatus } from '@reagent/axios-http-mock';

const httpMock = new HttpMock();
const axios = Axios.create({ adapter: httpMock.adapter });

From there, you can configure mock responses using the fluent interface:

httpMock.on('get').to('https://host.example/path').respondWith(200);

Match Modes

By default the mock implements strict matching, which means that all elements of the request that Axios sends must match the arguments to your mock request.

Keep in mind that Axios will always send an Accept header with the value 'application/json, text/plain, */*', which means you must override this in your request, or match this default value explicitly:

const httpMock = new HttpMock();

const axios = Axios.create({
  adapter: httpMock.adapter,
  baseURL: 'https://host.example',
});

httpMock
  .on('get')
  .to('https://host.example/path')
  .with({
    headers: { Accept: 'application/json, text/plain, */*' },
  })
  .respondWith(HttpStatus.OK);

expect(axios.get('/path', { params: { status: 'new' } })).rejects.toThrow(
  'No match found'
);

Alternatively, a partial match can match only on verb and URI:

const httpMock = new HttpMock({ matching: 'partial' });

const axios = Axios.create({
  adapter: httpMock.adapter,
  baseURL: 'https://host.example',
});

httpMock
  .on('get')
  .to('https://host.example/path')
  .respondWith(HttpStatus.OK, { key: 'value' });

const { status, data } = await axios.get('/path', {
  params: { status: 'new' },
});

expect(data).toEqual({ key: 'value' });
expect(status).toEqual(200);

Multiple Configured Matches

In partial match mode, the request that matches the most configured options is chosen:

// ...

httpMock
  .on('get')
  .to('https://host.example/path')
  .respondWith(HttpStatus.UNAUTHORIZED);

httpMock
  .on('get')
  .to('https://host.example/path')
  .with({ params: { status: 'new' } })
  .respondWith(HttpStatus.OK, { ok: 'pal' });

expect(
  axios.get('/path', { params: { status: 'new' } })
).resolves.toMatchObject({ status: 200 });

Raising Connectivity Errors

Using the specific timeout() method will cause a simulated network timeout error to be raised:

// ...

httpMock.on('get').to('https://host.example/path').timeout();
expect(axios.get('/path')).rejects.toThrow('Timeout');

Matching a Request Only Once

By default requests can be matched multiple times, but you can configure a single match and have subsequent requests fail:

httpMock
  .on('get')
  .to('https://host.example/path')
  .respondWith(HttpStatus.OK)
  .once();

expect(axios.get('/path')).resolves.toMatchObject({ status: 200 });
expect(axios.get('/path')).rejects.toThrow();

Resetting Configured Matches Between Tests

In the event that you have a shared test setup, you can easily clear all configured request methods by calling reset() in your before hook:

beforeEach(() => httpMock.reset());