Hubot test helper
Helper for testing Hubot script.
Install
npm install hubot-test-helper --save-dev
Usage
If you have a following hubot script:
module robot
You can test it like:
const Helper = ;// helper loads all scripts passed a directoryconst helper = './scripts'; // helper loads a specific script if it's a fileconst scriptHelper = './scripts/specific-script.js'; const co = ;const expect = expect; ;
HTTPD
By default Hubot enables a built in HTTP server. The server continues between
tests and so requires it to be shutdown during teardown using room.destroy()
.
This feature can be turned off in tests that don't need it by passing using
helper.createRoom(httpd: false)
.
See the tests for an example of testing the HTTP server.
Manual delay
Sometimes we can't access callback actions from a script. Just like in real use-case we may have to wait for a bot to finish processing before replying, in testing we may anticipate the delayed reply with a manual time delay.
For example we have the following script:
module robot;
To test the second callback response "ok2: ..." we use the following script:
const Helper = ;const helper = '../scripts/http.js'; const Promise = ;const co = ;const expect = expect; // test ping;
Note that yield
and generators are part of ECMA6, so it may not work on older node.js versions. It will wait for the delay to complete the beforeEach
before proceeding to the test it
.
Testing messages sent to other rooms
You can also test messages sent by your script to other rooms through Hubot's robot.messageRoom(...)
method.
Given the following script:
module robot
you could test the messages sent to other rooms like this:
const Helper = ;const helper = '../scripts/message-room.js'; const expect = expect; ;
Testing events
You can also test events emitted by your script. For example, Slack users may want to test the creation of a message attachment.
Given the following script:
module robot
you could test the emitted event like this:
const Helper = ;const helper = '../scripts/status_check.js'; const expect = expect; ;
Development
Requirements
- docker
- docker-compose
Setup
git clone https://github.com/mtsmfm/hubot-test-helper
cd hubot-test-helper
docker-compose up -d
docker-compose exec app bash
yarn install
Run test
yarn run test
Debug
yarn run test-unit-debug
Above command will output:
yarn run v0.18.1
$ mocha --inspect --debug-brk --compilers coffee:coffee-script/register test
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/59631086-0a0c-424b-8f5b-8828be123894
Then open chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/59631086-0a0c-424b-8f5b-8828be123894
in Chrome.