An AMQP
topic
exchange event listener that can be configured to listen for your event emitter's events and, optionally, return a configurable test result. Use this for integration testing of your event emitters.
When running integration tests of code that emits persistent messages to an AMQP
message queue via a topic
exchange, you don't want to leave messages in the queue, as that will cause confusion the next time you run your tests. Instead you need to be able to pull those messages from the queue.
A typical workflow often involves emitting a message designed to be consumed by another service, which will then emit a response message when it's done what it needs to do. To properly integration-test the entire round-trip it's desirable to have a mock service that emits a response message that your service listens for.
I've built this to test some specific microservices I am working on, and so appreciate it's not useful for a range of other scenarios, such as direct
or fanout
exchanges, or services that look for a replyTo
header to use to emit response messages to.
If you need this kind of flexibility feel free to fork this repo and contribute a PR. See the contributing notes for details.
Set the following environment variables
Variable | Default | Notes |
---|---|---|
NODE_ENV |
development |
Change this to test if using this to run against integration tests. |
AMQP_URL |
amqp://127.0.0.1 |
The url of the AMQP message queue to use. |
EXCHANGE |
amqp-test |
The name of the exchange. |
EVENTS |
none |
Base64 encoded JSON object describing the events to listen for and, optionally, the responses to emit. |
Supply an environment variable as follows
EVENTS=xxx
Where xxx
is a Base64 encoded JSON in the form:
{
[eventKey]: {
queue: 'queue to listen to',
response: 'event to respond with',
data: { some: 'data to respond with' }
},
...
}
EVENTS=eyJyZWdpc3RlciI6eyJxdWV1ZSI6IlJFR0lTVFJBVElPTiIsInJlc3BvbnNlIjoicmVnaXN0ZXJlZCIsImRhdGEiOnsidXJsIjoiaHR0cDovL2xvY2FsaG9zdDo5MDkwIn19LCJkaXNtaXNzIjp7InF1ZXVlIjoiRElTTUlTU0FMIn19
Decodes to
{
register: {
queue: 'REGISTRATION',
response: 'registered',
data: {
url: 'http://localhost:9090'
}
},
dismiss: {
queue: 'DISMISSAL'
}
}
This tells the event tester to
- listen for
register
on queueREGISTRATION
and, when it hears it, respond with keyregistered
and data{ url: 'http://localhost:9090' }
, and - listen for
dismiss
on queueDISMISSAL
, but do not respond.
If your event does not require any sort of response then you can simply leave out the response
and data
fields. This will tell the tester to just pull the message from the queue, so you don't end up with a load of stray messages littering your queue.
Branch | Tests | Code Coverage | Audit | Comments |
---|---|---|---|---|
develop |
Latest Staging Release | |||
main |
Latest Production Release |
- NodeJS, 8.10.0+ (I use
nvm
to manage Node versions —brew install nvm
.) - Docker (if on a Mac then use Docker for Mac, not the homebrew version)
npm install
You may add environment variables to a local .env
file
npm start
Note: The Dockerised version will run the script start.sh
to start the service, as this will wait for RabbitMQ
to start. This is important when running this on a CI system.
npm test
— runs the unit tests (quick)
or run npm run backend
to start the essential services, then:
npm run test:integration
— runs the integration tests (not so quick)
npm run lint
To auto-fix linting issues.
npm run lint -- --fix
This runs on every commit but you can do this manually as well via:
npm run prettier
Please see the contributing notes.