GithubHelp home page GithubHelp logo

rlugojr / hubot-test-helper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mtsmfm/hubot-test-helper

0.0 2.0 0.0 51 KB

Helper for testing hubot script

License: MIT License

Shell 1.68% Batchfile 0.77% CoffeeScript 97.55%

hubot-test-helper's Introduction

Hubot test helper

Build Status

Helper for testing Hubot script.

Install

npm install hubot-test-helper --save-dev

Usage

If you have a following hubot script:

module.exports = (robot) ->
  robot.respond /hi$/i, (msg) ->
    msg.reply 'hi'

You can test it like:

Helper = require('hubot-test-helper')
# helper loads all scripts passed a directory
helper = new Helper('./scripts')

# helper loads a specific script if it's a file
scriptHelper = new Helper('./scripts/specific-script.coffee')

co     = require('co')
expect = require('chai').expect

describe 'hello-world', ->

  beforeEach ->
    @room = helper.createRoom()

  afterEach ->
    @room.destroy()

  context 'user says hi to hubot', ->
    beforeEach ->
      co =>
        yield @room.user.say 'alice', '@hubot hi'
        yield @room.user.say 'bob',   '@hubot hi'

    it 'should reply to user', ->
      expect(@room.messages).to.eql [
        ['alice', '@hubot hi']
        ['hubot', '@alice hi']
        ['bob',   '@hubot hi']
        ['hubot', '@bob hi']
      ]

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.exports = (robot) ->
  robot.hear /(http(?:s?):\/\/(\S*))/i, (res) ->
    url = res.match[1]
    res.send "ok1: #{url}"
    robot.http(url).get() (err, response, body) ->
      res.send "ok2: #{url}"

To test the second callback response "ok2: ..." we use the following script:

Helper = require('hubot-test-helper')
helper = new Helper('../scripts/http.coffee')

Promise= require('bluebird')
co     = require('co')
expect = require('chai').expect

# test ping
describe 'http', ->
  beforeEach ->
    @room = helper.createRoom(httpd: false)

  # Test case
  context 'user posts link', ->
    beforeEach ->
      co =>
        yield @room.user.say 'user1', 'http://google.com'
        # delay one second for the second
        # callback message to be posted to @room
        yield new Promise.delay(1000)

    # response
    it 'expects deplayed callback from ok2', ->
      console.log @room.messages
      expect(@room.messages).to.eql [
        ['user1', 'http://google.com']
        ['hubot', 'ok1: http://google.com']
        ['hubot', 'ok2: http://google.com']
      ]

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 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.exports = (robot) ->

  robot.respond /check status$/i, (msg) ->
    robot.emit 'slack.attachment',
      message: msg.message,
      content: {
        color: "good"
        text: "It's all good!"
      }

you could test the emitted event like this:

Helper = require 'hubot-test-helper'
helper = new Helper('../scripts/status_check.coffee')

expect = require('chai').expect

describe 'status check', ->
  beforeEach ->
    @room = helper.createRoom(httpd: false)

  it 'should send a slack event', ->
    response = null
    @room.robot.on 'slack.attachment', (event) ->
      response = event.content

    @room.user.say('bob', '@hubot check status').then =>
      expect(response.text).to.eql("It's all good!")

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.

hubot-test-helper's People

Contributors

akatz avatar amussey avatar atmos avatar floby avatar harlantwood avatar kengz avatar miaonster avatar mtsmfm avatar pchaigno avatar sukima avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.