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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mock-asap

v0.0.0

Published

Server mocks made to be easy

Readme

Mock ASAP (as soon as possible) Build Status

Idea

Idea of this package is to get package ready to easily setup http stubs for functional and integration testing.

This package consists of a proxy programmable via Sinon.JS stubs

Install

npm install --save mock-asap

Note: you should have Node.js with support of ES6 (at least v4.0)

Example

Here is an example how you can use mock-asap with Nightmare.js:

const mockAsap = require('./index.js');
const Nightmare = require('nightmare');
const sinon = require('sinon');
const match = sinon.match;

const nightmare = Nightmare({
    show: true,
    switches: {
        'proxy-server': '127.0.0.1:8889',
        'ignore-certificate-errors': true
    }
});

mockAsap.start()
    .then(function() {
        mockAsap.stub.http.withArgs(
            match.has('url', match('custom-avito.html'))
        ).returns(
            mockAsap.respondWith.file(__dirname + '/custom-avito.html')
        );

        return nightmare
            .goto('http://avito.ru/custom-avito.html')
            .evaluate(() => document.body.innerHTML);
    })
    .then(bodyHtml => console.log(bodyHtml))
    .then(() => nightmare.end())
    .then(() => mockAsap.stop());

This package uses port 8889 for communication. In order to work properly it should be free before running. Later this package will have ability to configure occupied port

Docs

mockAsap.start()

mockAsap.start starts stubs proxy server. It returns promise which will be resolved when proxy will listen for incoming messages.

Note: for now system designed in the way that permits running only one instance of mockAsap. So you should not try to run mockAsap.start() several times.

mockAsap.stop()

mockAsap.start stops stubs proxy server and browser. It returns promise which will be resolved when everything was stopped.

mockAsap.stub

mockAsap.stub contains Sinon.JS stubs used for programming proxy server behavior. It contains http and https stubs used in this way:

stub.https.withArgs(
    sinon.match.has('url', sinon.match('/rest/text/terms/'))
).returns(
    ({ proxyToClientResponse: res }) => {
        res.setHeader('Content-Type', 'text/html');
        res.end(htmlText);
    }
);

Stub argument used for getting result function - is an incoming ClientRequest (see Node.js documentation). The result function will with http-mitm-proxy Context object

There is also stubs.reset() synchronous method which resets stubs to their default behavior (i.e. just proxying).

Note: you should not store stubs.https and stubs.http to variables because otherwise everything will be broken after stubs.reset()

mockAsap.match

mockAsap.match contains helpers for simpler matching against often used rules.

mockAsap.match.url(url)

Matches if request contains url as a substring

mockAsap.stub.https.withArgs(
    mockAsap.match.url('logo-avito.svg')
).returns(
    mockAsap.respondWith.file(path.join(__dirname, 'avito/logo-avito.svg'))
);

mockAsap.respondWith

mockAsap.respondWith contains helpers for simpler responding with popular type of responses.

mockAsap.respondWith.text(text)

mockAsap.respondWith.text(text) responds with text as plain text

mockAsap.stub.https.withArgs(
    sinon.match.any
).returns(
    mockAsap.respondWith.text('Hello world!')
);

mockAsap.respondWith.html(html)

mockAsap.respondWith.html(html) responds with html as html document

mockAsap.stub.https.withArgs(
    mockAsap.match.url('index.html')
).returns(
    mockAsap.respondWith.html('<h1>Hello world!</h1>')
);

mockAsap.respondWith.json(jsObject)

mockAsap.respondWith.json(jsObject) stringifies jsObject and sends it as json

mockAsap.stub.https.withArgs(
    mockAsap.match.url('/1.json')
).returns(
    mockAsap.respondWith.json({ hello: 'world' })
);

mockAsap.respondWith.jsonTransformer(responseTransformer[, requestBodyTransformer])

mockAsap.respondWith.jsonTransformer(responseTransformer) firstly tries to get original response from server. After all data has been received, it parses response as JSON and passes it as first argument of responseTransformer function. After responseTransformer was called it passes modified data to browser (i.e. to Chrome). For example, the code below will transform request /1.json {"foo": 1, "baz": 2} to {"foo": "bar", "baz": 2}

mockAsap.stub.https.withArgs(
    mockAsap.match.url('/1.json')
).returns(
    mockAsap.respondWith.jsonTransformer(json => {
        json.foo = 'bar';
    })
);

requestBodyTransformer works in the same way. The only difference is that it tries to transform request body as JSON. For example, you may want to suppress some fields which produce some pollution or computations you would like to avoid. You can do it in this way

mockAsap.stub.https.withArgs(
    mockAsap.match.url('/2.json')
).returns(
    mockAsap.respondWith.jsonTransformer(null, json => {
        delete json.fieldIWantToHide;
    })
);

Note: transformer assumed to be a dirty function, not a pure one

mockAsap.respondWith.file(absolutePathToFile)

mockAsap.respondWith.file(absolutePathToFile) responds with content of absolutePathToFile

stub.https.withArgs(
    mockAsap.match.url('/res/7EiNlv7G_KCvanpivhp5XQ.jpg')
).returns(
    respondWith.file(path.join(__dirname, 'actiagent/meow.jpg'))
);

mockAsap.respondWith.serveStatic(pathToCut, absolutePathToDir)

mockAsap.respondWith.serveStatic(pathToCut, absolutePathToDir) replaces drops pathToCut and prepends absolutePathToDir to the rest

// Here /public/pics/1.jpeg will be answered with content of ../../pictures/1.jpeg
mockAsap.stub.https.withArgs(
    mockAsap.match.url('/public/pics')
).returns(
    mockAsap.respondWith.json('/public/pics', path.join(__dirname, '../../pictures'))
);

License

MIT