{"id":13583776,"url":"https://github.com/mtsmfm/hubot-test-helper","last_synced_at":"2025-04-06T01:10:46.406Z","repository":{"id":17619581,"uuid":"20423759","full_name":"mtsmfm/hubot-test-helper","owner":"mtsmfm","description":"Helper for testing hubot script","archived":false,"fork":false,"pushed_at":"2023-12-29T10:47:42.000Z","size":73,"stargazers_count":115,"open_issues_count":13,"forks_count":41,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-25T00:01:06.023Z","etag":null,"topics":["coffeescript","hubot","hubot-scripts"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mtsmfm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-06-02T23:52:24.000Z","updated_at":"2023-08-18T17:38:54.000Z","dependencies_parsed_at":"2024-01-24T13:07:03.435Z","dependency_job_id":null,"html_url":"https://github.com/mtsmfm/hubot-test-helper","commit_stats":{"total_commits":100,"total_committers":17,"mean_commits":5.882352941176471,"dds":0.38,"last_synced_commit":"37b5e99faa2023c95d88c420b2b3bb1f09dd07b1"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtsmfm%2Fhubot-test-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtsmfm%2Fhubot-test-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtsmfm%2Fhubot-test-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtsmfm%2Fhubot-test-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtsmfm","download_url":"https://codeload.github.com/mtsmfm/hubot-test-helper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247419861,"owners_count":20936012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coffeescript","hubot","hubot-scripts"],"created_at":"2024-08-01T15:03:47.142Z","updated_at":"2025-04-06T01:10:46.391Z","avatar_url":"https://github.com/mtsmfm.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Hubot test helper\n\n[![Build Status](https://travis-ci.org/mtsmfm/hubot-test-helper.svg?branch=master)](https://travis-ci.org/mtsmfm/hubot-test-helper)\n\nHelper for testing Hubot script.\n\n## Install\n\n`npm install hubot-test-helper --save-dev`\n\n## Usage\n\nIf you have a following hubot script:\n\n```javascript\nmodule.exports = robot =\u003e\n  robot.respond(/hi$/i, msg =\u003e msg.reply('hi'))\n```\n\nYou can test it like:\n\n```javascript\nconst Helper = require('hubot-test-helper');\n// helper loads all scripts passed a directory\nconst helper = new Helper('./scripts');\n\n// helper loads a specific script if it's a file\nconst scriptHelper = new Helper('./scripts/specific-script.js');\n\nconst co     = require('co');\nconst expect = require('chai').expect;\n\ndescribe('hello-world', function() {\n  beforeEach(function() {\n    this.room = helper.createRoom();\n  });\n  afterEach(function() {\n    this.room.destroy();\n  });\n\n  context('user says hi to hubot', function() {\n    beforeEach(function() {\n      return co(function*() {\n        yield this.room.user.say('alice', '@hubot hi');\n        yield this.room.user.say('bob',   '@hubot hi');\n      }.bind(this));\n    });\n\n    it('should reply to user', function() {\n      expect(this.room.messages).to.eql([\n        ['alice', '@hubot hi'],\n        ['hubot', '@alice hi'],\n        ['bob',   '@hubot hi'],\n        ['hubot', '@bob hi']\n      ]);\n    });\n  });\n});\n```\n\n#### HTTPD\n\nBy default Hubot enables a built in HTTP server. The server continues between\ntests and so requires it to be shutdown during teardown using `room.destroy()`.\n\nThis feature can be turned off in tests that don't need it by passing using\n`helper.createRoom(httpd: false)`.\n\nSee [the tests](test/httpd-world_test.js) for an example of testing the\nHTTP server.\n\n\n#### Manual delay\n\nSometimes we can't access callback actions from a script.\nJust like in real use-case we may have to wait for a bot to finish processing before replying,\nin testing we may anticipate the delayed reply with a manual time delay.\n\nFor example we have the following script:\n\n```javascript\nmodule.exports = robot =\u003e\n  robot.hear(/(http(?:s?):\\/\\/(\\S*))/i, res =\u003e {\n    const url = res.match[1];\n    res.send(`ok1: ${url}`);\n    robot.http(url).get()((err, response, body) =\u003e res.send(`ok2: ${url}`));\n  });\n```\n\nTo test the second callback response \"ok2: ...\" we use the following script:\n\n```javascript\nconst Helper = require('hubot-test-helper');\nconst helper = new Helper('../scripts/http.js');\n\nconst Promise = require('bluebird');\nconst co      = require('co');\nconst expect  = require('chai').expect;\n\n// test ping\ndescribe('http', function() {\n  beforeEach(function() {\n    this.room = helper.createRoom({httpd: false});\n  });\n\n  // Test case\n  context('user posts link', function() {\n    beforeEach(function() {\n      return co(function*() {\n        yield this.room.user.say('user1', 'http://google.com');\n        // delay one second for the second\n        // callback message to be posted to @room\n        yield new Promise.delay(1000);\n      }.bind(this));\n    });\n\n    // response\n    it('expects deplayed callback from ok2', function() {\n      console.log(this.room.messages);\n      expect(this.room.messages).to.eql([\n        ['user1', 'http://google.com'],\n        ['hubot', 'ok1: http://google.com'],\n        ['hubot', 'ok2: http://google.com']\n      ]);\n    });\n  });\n});\n```\n\nNote that `yield` and *generators* are part of [**ECMA6**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*), 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`.\n\n\n#### Testing messages sent to other rooms\n\nYou can also test messages sent by your script to other rooms through Hubot's `robot.messageRoom(...)` method.\n\nGiven the following script:\n```javascript\nmodule.exports = robot =\u003e\n  robot.respond(/announce otherRoom: (.+)$/i, msg =\u003e {\n    robot.messageRoom('otherRoom', \"@#{msg.envelope.user.name} said: #{msg.msg.match[1]}\");\n  })\n```\n\nyou could test the messages sent to other rooms like this:\n```javascript\nconst Helper = require('../src/index');\nconst helper = new Helper('../scripts/message-room.js');\n\nconst expect = require('chai').expect;\n\ndescribe('message-room', function() {\n  beforeEach(function() {\n    this.room = helper.createRoom({name: 'room', httpd: false});\n  });\n\n  context('user asks hubot to announce something', function() {\n    beforeEach(function() {\n      return co(function*() {\n        yield this.room.user.say('alice', '@hubot announce otherRoom: I love hubot!');\n      }.bind(this));\n    });\n\n    it('should not post to this channel', function() {\n      expect(this.room.messages).to.eql([\n        ['alice', '@hubot announce otherRoom: I love hubot!']\n      ]);\n    });\n\n    it('should post to the other channel', function() {\n      expect(this.room.robot.messagesTo['otherRoom']).to.eql([\n        ['hubot', '@alice says: I love hubot!']\n      ]);\n    });\n  });\n});\n```\n\n\n#### Testing events\n\nYou can also test events emitted by your script.  For example, Slack users\nmay want to test the creation of a\n[message attachment](https://api.slack.com/docs/attachments).\n\nGiven the following script:\n\n```javascript\nmodule.exports = robot =\u003e\n  robot.respond(/check status$/i, msg =\u003e\n    robot.emit('slack.attachment', {\n      message: msg.message,\n      content: {\n        color: \"good\",\n        text: \"It's all good!\"\n      }\n    })\n  )\n```\n\nyou could test the emitted event like this:\n\n```javascript\nconst Helper = require('hubot-test-helper');\nconst helper = new Helper('../scripts/status_check.js');\n\nconst expect = require('chai').expect;\n\ndescribe('status check', function() {\n  beforeEach(function() {\n    this.room = helper.createRoom({httpd: false});\n  });\n\n  it('should send a slack event', function() {\n    let response = null;\n    this.room.robot.on('slack.attachment', event =\u003e response = event.content);\n\n    this.room.user.say('bob', '@hubot check status').then(() =\u003e {\n      expect(response.text).to.eql(\"It's all good!\");\n    });\n  });\n});\n```\n\n## Development\n\n### Requirements\n\n- docker\n- docker-compose\n\n### Setup\n\n```\ngit clone https://github.com/mtsmfm/hubot-test-helper\ncd hubot-test-helper\ndocker-compose up -d\ndocker-compose exec app bash\nyarn install\n```\n\n### Run test\n\n```\nyarn run test\n```\n\n#### Debug\n\n```\nyarn run test-unit-debug\n```\n\nAbove command will output:\n\n```\nyarn run v0.18.1\n$ mocha --inspect --debug-brk --compilers coffee:coffee-script/register test\nDebugger listening on port 9229.\nWarning: This is an experimental feature and could change at any time.\nTo start debugging, open the following URL in Chrome:\n    chrome-devtools://devtools/bundled/inspector.html?experiments=true\u0026v8only=true\u0026ws=127.0.0.1:9229/59631086-0a0c-424b-8f5b-8828be123894\n```\n\nThen open `chrome-devtools://devtools/bundled/inspector.html?experiments=true\u0026v8only=true\u0026ws=127.0.0.1:9229/59631086-0a0c-424b-8f5b-8828be123894` in Chrome.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtsmfm%2Fhubot-test-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtsmfm%2Fhubot-test-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtsmfm%2Fhubot-test-helper/lists"}