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

axios-stubber

v1.0.7-alpha.1

Published

Simplified axios request and response stubbing

Downloads

14

Readme

axios-stubber

axios-stubber version

Simplified axios request and response stubbing.

Stubs axios globally, making its calls return as specified in stubs files (or objects).

npm i -D axios-stubber
yarn add --dev axios-stubber

axiosStubber

import axios from 'axios';
import axiosStubber from 'axios-stubber';

test('axios-stubber example', async () => {
    axiosStubber(axios, {
        request: {
            method: "GET",
            url: "https://w00t.com"
        },
        response: {
            status: 201,
            body: {
                yes: "It is"
            }
        }
    }); // the argument could also be an array, a json file, or js file 

    let {data, status} = await axios.get("https://w00t.com");

    expect(data).toStrictEqual({yes: "It is"});
    expect(status).toStrictEqual(201);
});

Test file:

const go = require('./go');

import axios from 'axios';
import axiosStubber from 'axios-stubber';

describe('axiosStubber', () => {

    let axiosMockControl;
    afterEach(() => {
        if (axiosMockControl) axiosMockControl.restore();
    });

    it('get', async () => {
        axiosMockControl = axiosStubber(axios, __dirname + '/stubs/exampleApi.stubs.json');

        let {data} = await axios.get("http://master.example.com/info?code=123");
        expect(data).toStrictEqual({result: 123456});
    });

    it('post', async () => {
        axiosMockControl = axiosStubber(axios, __dirname + '/stubs/exampleApi.stubs.json');

        let r = await go();
        expect(r).toStrictEqual({yay: true});
    });

    it('get', async () => {
        axiosMockControl = axiosStubber(axios, [__dirname + '/stubs/exampleApi.stubs.json']);

        let {data} = await axios.get("http://master.example.com/info?code=123");
        expect(data).toStrictEqual({result: 123456});
    });

    it('get', async () => {
        axiosMockControl = axiosStubber(axios, [
            {
                request: {
                    method: "GET",
                    url: "https://w00t.com"
                },
                response: {
                    status: 201,
                    body: {
                        yes: "It is"
                    }
                }
            }
        ]);

        let {data, status} = await axios.get("https://w00t.com");
        expect(data).toStrictEqual({yes: "It is"});
        expect(status).toStrictEqual(201);
    });

});

axiosStubsRecorder

Intercepts all axios calls and generates a stubs file for later use.

import { axiosStubsRecorder } from 'axios-stubber';

test('t1 - save to new file', async () => {
    const axiosMockControl = axiosStubsRecorder(axios, 'my.stubs.json');

    let {data} = await axios.get("https://reqres.in/api/users/3");

    expect(data.data.first_name).toEqual('Emma');
    expect(data.data.last_name).toEqual('Wong');

    // you can now check that 'my.stubs.json' has all requests and responses recorded
    
    axiosMockControl.restore();
});