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

pubnub-functions-mock

v0.0.17

Published

Mocks PubNub Functions modules for unit testing

Downloads

16

Readme

PubNub Functions Mock

Build Status Coverage Status Known Vulnerabilities

Unit test PubNub Functions event handlers on your local machine

Currently supported modules for mock (docs here):

  • XHR (currently makes real requests using https://www.npmjs.com/package/node-fetch)
  • KV Store
  • codec/query_string
  • codec/base64
  • PubNub
  • Vault

Any module can be overridden using overrideDefaultModules within a single test body. The module or modules will only be overridden in that single test block.

endpoint.overrideDefaultModules({
    "xhr" : () => {
        return Promise.resolve(200);
    }
});

To override a default module in all tests, pass the module object when the Event Handler is initialized.

endpoint = Mock('./myEndpointEventHandler.js', { 
    "xhr" : () => {
        return Promise.resolve(200);
    }
});

Mock the KVStore for a test

endpoint.mockKVStoreData({"key":"value"});

Mock the KVStore counters for a test

endpoint.mockKVStoreCounters({"key":123});

Example PubNub Function Endpoint unit test with Mocha and Chai

// myTest.js
const assert = require('chai').assert;
const Mock = require('pubnub-functions-mock');

const endpointRequestObject = {
    "body": "{}",
    "message": {},
    "method": null,
    "params": {}
};

const endpointResponseObject = {
    "headers": {},
    "status": 200,
    "send": function ( body ) {
        return new Promise( (resolve, reject) => {
            resolve({
                "body": body || "",
                "status": this.status
            });
        });
    }
};

describe('#endpoint', () => {
    let endpoint;

    beforeEach(() => {
        endpoint = Mock('./myEndpointEventHandler.js');
    });

    it('creates endpoint event handler of type Function', (done) => {
        assert.isFunction(endpoint, 'was successfully created');
        done();
    });

    it('returns "Hello World!"', (done) => {
        
        let request = Object.assign({}, endpointRequestObject);
        let response = Object.assign({}, endpointResponseObject);

        let correctResult = {
            "body": "Hello World!",
            "status": 200 
        };

        endpoint(request, response).then((testResult) => {

            assert.equal(testResult.status, correctResult.status, 'status');
            assert.equal(testResult.body, correctResult.body, 'response body');

            done();
        });
    });

    it('returns a kvstore value', (done) => {
        
        let request = Object.assign({}, endpointRequestObject);
        let response = Object.assign({}, endpointResponseObject);

        request.getKvValue = true;

        let preExistingValue = { "key" : "value" };

        let correctResult = {
            "body": preExistingValue.key,
            "status": 200 
        };

        // Mock the pre-existing KVStore value for this test only
        endpoint.mockKVStoreData(preExistingValue);

        endpoint(request, response).then((testResult) => {

            assert.equal(testResult.status, correctResult.status, 'status');
            assert.equal(testResult.body, correctResult.body, 'response body');

            done();
        });
    });
});

The above test would be run on myEndpointEventHandler.js using mocha myTest

// myEndpointEventHandler.js
export default (request, response) => {
    const pubnub = require('pubnub');
    const kvstore = require('kvstore');

    if (request.getKvValue) {
        return kvstore.get('key').then((value) => {
            response.status = 200;
            return response.send(value);
        });
    }

    response.status = 200;
    return response.send("Hello World!");
};