{"id":15622433,"url":"https://github.com/ajb413/pubnub-functions-mock","last_synced_at":"2026-02-27T12:02:47.456Z","repository":{"id":78009932,"uuid":"108607742","full_name":"ajb413/pubnub-functions-mock","owner":"ajb413","description":"Mocks PubNub Functions modules for event handler unit testing","archived":false,"fork":false,"pushed_at":"2019-05-14T22:17:08.000Z","size":50,"stargazers_count":6,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T21:02:36.428Z","etag":null,"topics":["chai","javascript","mocha","nodejs","pubnub","pubnub-functions","serverless","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ajb413.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-28T00:27:37.000Z","updated_at":"2019-10-31T04:29:06.000Z","dependencies_parsed_at":"2023-03-08T14:00:26.310Z","dependency_job_id":null,"html_url":"https://github.com/ajb413/pubnub-functions-mock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajb413%2Fpubnub-functions-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajb413%2Fpubnub-functions-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajb413%2Fpubnub-functions-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajb413%2Fpubnub-functions-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajb413","download_url":"https://codeload.github.com/ajb413/pubnub-functions-mock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251334182,"owners_count":21572963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chai","javascript","mocha","nodejs","pubnub","pubnub-functions","serverless","unit-testing"],"created_at":"2024-10-03T09:53:59.576Z","updated_at":"2026-02-27T12:02:47.403Z","avatar_url":"https://github.com/ajb413.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PubNub Functions Mock \n\n[![Build Status](https://travis-ci.org/ajb413/pubnub-functions-mock.svg?branch=master)](https://travis-ci.org/ajb413/pubnub-functions-mock)\n[![Coverage Status](https://coveralls.io/repos/github/ajb413/pubnub-functions-mock/badge.svg?branch=master\u0026c=b)](https://coveralls.io/github/ajb413/pubnub-functions-mock?branch=master\u0026c=b)\n[![Known Vulnerabilities](https://snyk.io/test/github/ajb413/pubnub-functions-mock/badge.svg)](https://snyk.io/test/github/ajb413/pubnub-functions-mock)\n\nUnit test PubNub Functions event handlers on your local machine\n\n## Currently supported modules for mock ([docs here](https://www.pubnub.com/docs/blocks/xhr-module)):\n- XHR (currently makes **real** requests using https://www.npmjs.com/package/node-fetch)\n- KV Store\n- codec/query_string\n- codec/base64\n- PubNub\n- Vault\n\nAny module can be overridden using `overrideDefaultModules` within a single test body. The module or modules will only be overridden in that single test block.\n```javascript\nendpoint.overrideDefaultModules({\n    \"xhr\" : () =\u003e {\n        return Promise.resolve(200);\n    }\n});\n```\n\nTo override a default module in all tests, pass the module object when the Event Handler is initialized.\n```javascript\nendpoint = Mock('./myEndpointEventHandler.js', { \n    \"xhr\" : () =\u003e {\n        return Promise.resolve(200);\n    }\n});\n```\n\nMock the KVStore for a test\n```javascript\nendpoint.mockKVStoreData({\"key\":\"value\"});\n```\n\nMock the KVStore counters for a test\n```javascript\nendpoint.mockKVStoreCounters({\"key\":123});\n```\n\n## Example PubNub Function Endpoint unit test with Mocha and Chai\n```javascript\n// myTest.js\nconst assert = require('chai').assert;\nconst Mock = require('pubnub-functions-mock');\n\nconst endpointRequestObject = {\n    \"body\": \"{}\",\n    \"message\": {},\n    \"method\": null,\n    \"params\": {}\n};\n\nconst endpointResponseObject = {\n    \"headers\": {},\n    \"status\": 200,\n    \"send\": function ( body ) {\n        return new Promise( (resolve, reject) =\u003e {\n            resolve({\n                \"body\": body || \"\",\n                \"status\": this.status\n            });\n        });\n    }\n};\n\ndescribe('#endpoint', () =\u003e {\n    let endpoint;\n\n    beforeEach(() =\u003e {\n        endpoint = Mock('./myEndpointEventHandler.js');\n    });\n\n    it('creates endpoint event handler of type Function', (done) =\u003e {\n        assert.isFunction(endpoint, 'was successfully created');\n        done();\n    });\n\n    it('returns \"Hello World!\"', (done) =\u003e {\n        \n        let request = Object.assign({}, endpointRequestObject);\n        let response = Object.assign({}, endpointResponseObject);\n\n        let correctResult = {\n            \"body\": \"Hello World!\",\n            \"status\": 200 \n        };\n\n        endpoint(request, response).then((testResult) =\u003e {\n\n            assert.equal(testResult.status, correctResult.status, 'status');\n            assert.equal(testResult.body, correctResult.body, 'response body');\n\n            done();\n        });\n    });\n\n    it('returns a kvstore value', (done) =\u003e {\n        \n        let request = Object.assign({}, endpointRequestObject);\n        let response = Object.assign({}, endpointResponseObject);\n\n        request.getKvValue = true;\n\n        let preExistingValue = { \"key\" : \"value\" };\n\n        let correctResult = {\n            \"body\": preExistingValue.key,\n            \"status\": 200 \n        };\n\n        // Mock the pre-existing KVStore value for this test only\n        endpoint.mockKVStoreData(preExistingValue);\n\n        endpoint(request, response).then((testResult) =\u003e {\n\n            assert.equal(testResult.status, correctResult.status, 'status');\n            assert.equal(testResult.body, correctResult.body, 'response body');\n\n            done();\n        });\n    });\n});\n```\n\nThe above test would be run on `myEndpointEventHandler.js` using\n`mocha myTest`\n\n```javascript\n// myEndpointEventHandler.js\nexport default (request, response) =\u003e {\n    const pubnub = require('pubnub');\n    const kvstore = require('kvstore');\n\n    if (request.getKvValue) {\n        return kvstore.get('key').then((value) =\u003e {\n            response.status = 200;\n            return response.send(value);\n        });\n    }\n\n    response.status = 200;\n    return response.send(\"Hello World!\");\n};\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajb413%2Fpubnub-functions-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajb413%2Fpubnub-functions-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajb413%2Fpubnub-functions-mock/lists"}