Local testing of your code
Let's look at testing the
WebSocketWrapper.js
file. At the top of it, it requires nativescript-websockets
code. Well, the nativescript-websockets
code only runs on Android or iOS. It doesn't run on your development machine. So, how are you going to test it?
You will use Proxyquire. Proxyquire is a Nifty library that allows you to replace any require
statements during tests without modifying the code that you are testing. Using it allows you to dynamically replace the functions and classes exported from a module with mock or dummy replacements. A require
statement would load the exported functions and classes into your code. So, let's look at the top part of the testWebSockets.js
file:
var proxyquire = require('proxyquire'); var assert = require('assert'); var DummyWS = function() { this._isOpen = false; this._events = {}; }; DummyWS.prototype.on = function(event, callback) { if (!this._events[event]) { this._events[event] = [];} this._events[event].push...