Mock implementation of ExoCom in JavaScript
a mock implementation of ExoCom-Dev for sending and receiving messages to your ExoServices in tests
Installation
$ npm i --save-dev exocom-mock
Usage
ExoComMock is for testing ExoServices, so all of the code below would be located in the test suite for an ExoService implementation, and run inside the directory of an ExoService implementation. In this case we will use this example ExoService.
Create an ExoComMock instance
First we create an instance of ExoComMock, to simulate an Exosphere environment in our test suite.
const ExoComMock = require('exocom-mock')
const exocom = new ExoComMock
exocom.listen()
You can also provide a port to listen
if you want to use a specific one.
Boot up the Exoservice to be tested
Next we boot up the ExoService we want to test, and tell it to use our ExoComMock instance. This could look something like:
const ExoService = require('exoservice')
const exoservice = new ExoService({ serviceName: 'ping service',
root: 'tut-run/ping-service',
exocomHost: 'localhost',
exocomPort: this.exocom.port })
exoservice.listen()
The Exoservice automatically connects to the Exocom server, so all that our MockExocom instance has to do is wait until that happens:
exocom.waitForService(<CALLBACK>)
Communicating with the ExoService
First, let's send a simple message to our ExoService:
exocom.send({ service: 'ping service',
name: 'ping',
messageId: '123',
payload: { foo: 'bar' } })
The fields are:
- name: the name of the message
- messageId: (optional) UUID of the message to send.
- payload: (optional) payload to send with the message.
The "ping" exoservice replies to "ping" messages with a "pong" message. Let's wait for it:
exocom.onReceive(<CALLBACK>)
And once it has arrived, verify it:
const expect = require('chai').expect
const receivedMessage = this.exocom.receivedMessages[0]
expect(receivedMessage.name).to.equal('pong')
expect(receivedMessage.payload).to.eql({ foo: 'bar' })
The pattern above is useful when sending a command and verifying its response is done in separate steps of your test. You can also do both at the same time:
exocom.send({service: 'ping service', name: 'ping' }, function(reply) {
// ...
})
You can reset the list of received messages by the MockExoCom instance:
exocom.reset()
Close the instance
At the end of a test, close your MockExoCom instance so that you can create a fresh one at the same port in your next test.
exocom.close()
Development
See our developer documentation