PubNub Functions Mock
Unit test PubNub Functions event handlers on your local machine
docs here):
Currently supported modules for mock (- 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;
To override a default module in all tests, pass the module object when the Event Handler is initialized.
endpoint = ;
Mock the KVStore for a test
endpoint;
Mock the KVStore counters for a test
endpoint;
Example PubNub Function Endpoint unit test with Mocha and Chai
// myTest.jsconst assert = assert;const Mock = ; const endpointRequestObject = "body": "{}" "message": {} "method": null "params": {}; const endpointResponseObject = "headers": {} "status": 200 { return { ; }; }; ;
The above test would be run on myEndpointEventHandler.js
using
mocha myTest
// myEndpointEventHandler.js { const pubnub = ; const kvstore = ; if requestgetKvValue return kvstore; responsestatus = 200; return response;};