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 🙏

© 2025 – Pkg Stats / Ryan Hefner

express-mock-backend

v0.1.2-beta.6

Published

Express mock rest api server for testing

Downloads

2

Readme

express-mock-backend

Launching the Mock server

var MockBackend = require("express-mock-rest-api").MockBackend;
var server = new MockBackend(8999,"/mock-api",{});
server.start();

Writing a integration test for your application

In this example I'm using the utils provided in "mock-response-handler". It's a http client calling the mock server rest-api.


import {MockBackendApi} from "mock-response-handler";
import * as axios from "axios";
import {expect} from "chai";

To be able to use the utils provided in the mock-response-handler we need to create an HttpHandler The HttpHandler need to implement two methods: doGet(url,headers) & doPost(url,entity,headers) In this example I'm using axios http library

class ExampleHttpClient {
    /**
     *
     * @param url - full url to the rest endpoint
     * @param headers
     * @returns {Promise} promise resolving the json from the response
     */
    doGet(url,headers) {
        return this._toJSON(axios.get(url,headers ? { headers : headers } : null))
    }

    /**
     *
     * @param url
     * @param entity
     * @param headers
     * @returns {*}
     */
    doPost(url,entity, headers) {
        return this._toJSON(axios.post(url,entity, headers ? { headers : headers } : null));
    }
    _toJSON(promise) {
        return new Promise((resolve,reject)=>{
            promise.then(response => {
                resolve(response.data);
            }).catch( err => {
                reject(err);
            })
        });
    }
}

Sample service, what you eventually will test in your real code

class SampleService {
    fetchUsers() {
        return new Promise((resolve,reject)=>{
            axios.get("http://localhost:8999/mock-api/users").then( response => {
               resolve(response.data);
            }).catch( err => {
                reject(err);
            });
        })
    }
}

Mocha test using chai

/**
 * Mocha test using chai
 */
describe("Sample test using express mock rest api",()=>{

    var mockServerApi;
    var sampleService = new SampleService();
    beforeEach(()=>{
        mockServerApi = new MockBackendApi(new ExampleHttpClient(), "http://localhost:8999","/responses", "/mock-api");
        var mockResponse = mockServerApi.mockResponse();

        //the response I want from my mocked rest api
        var mockedResponse = [
            { username : "Pete"},
            { username : "Fred "}
        ];

        //tell the mock server to return the mocked response for the path /users
        return mockResponse.path("/users")
            .status(200)
            .method("GET")
            .body(mockedResponse)
            .execute();

    });
    afterEach(()=>{
        //reset the mock server
        return mockServerApi.reset();
    });

    it("Test sample request to mocked endpoint", ()=>{
        return sampleService.fetchUsers().then( users => {
            expect(users.length).to.equal(2);
            expect(users[0].username).to.equal("Pete");
        });
    });

    it("Example on how to verify the request", ()=>{
        return sampleService.fetchUsers().then( users => {
            expect(users.length).to.equal(2);
            expect(users[0].username).to.equal("Pete");
        }).then(() => {
            //verify the request your application (SampleService) sent
            return mockServerApi.getResponses("/users","GET").then( responses => {
                //all the responses that you have mocked
                expect(responses.length).to.equal(1);
                expect(responses[0].responseHttpStatus).to.equal(200);

                //all requests that have been mapped to this response
                expect(responses[0].requests.length).to.equal(1);
                expect(responses[0].requests[0].method).to.equal("GET");
            });
        }).then(() => {
            //since we only have one mocked response this will have the same responses as previous .then
            return mockServerApi.getResponses().then( responses => {
                expect(responses.length).to.equal(1);
            });
        });;
    });

});