Skip to content

Commit

Permalink
perf: es2015
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsmfm committed Oct 8, 2017
1 parent fe2f0fe commit 6a621be
Show file tree
Hide file tree
Showing 26 changed files with 290 additions and 351 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM node
FROM node:4

RUN npm install -g yarn
196 changes: 105 additions & 91 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,50 @@ Helper for testing Hubot script.

If you have a following hubot script:

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

You can test it like:

```coffee
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']
```javascript
const Helper = require('hubot-test-helper');
// helper loads all scripts passed a directory
const helper = new Helper('./scripts');

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

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

describe('hello-world', function() {
beforeEach(function() {
this.room = helper.createRoom();
});
afterEach(function() {
this.room.destroy();
});

context('user says hi to hubot', function() {
beforeEach(function() {
return co(function*() {
yield this.room.user.say('alice', '@hubot hi');
yield this.room.user.say('bob', '@hubot hi');
}.bind(this));
});

it('should reply to user', function() {
expect(this.room.messages).to.eql([
['alice', '@hubot hi'],
['hubot', '@alice hi'],
['bob', '@hubot hi'],
['hubot', '@bob hi']
]
]);
});
});
});
```

#### HTTPD
Expand All @@ -62,7 +66,7 @@ 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](test/httpd-world_test.coffee) for an example of testing the
See [the tests](test/httpd-world_test.js) for an example of testing the
HTTP server.


Expand All @@ -74,47 +78,53 @@ in testing we may anticipate the delayed reply with a manual time delay.

For example we have the following script:

```coffee
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}"
```javascript
module.exports = robot =>
robot.hear(/(http(?:s?):\/\/(\S*))/i, res => {
const 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:

```coffee
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']
```javascript
const Helper = require('hubot-test-helper');
const helper = new Helper('../scripts/http.js');

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

// test ping
describe('http', function() {
beforeEach(function() {
this.room = helper.createRoom({httpd: false});
});

// Test case
context('user posts link', function() {
beforeEach(function() {
return co(function*() {
yield this.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);
}.bind(this));
});

// response
it('expects deplayed callback from ok2', function() {
console.log(this.room.messages);
expect(this.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**](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`.
Expand All @@ -128,37 +138,41 @@ may want to test the creation of a

Given the following script:

```coffee
module.exports = (robot) ->

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

you could test the emitted event like this:

```coffee
Helper = require 'hubot-test-helper'
helper = new Helper('../scripts/status_check.coffee')
```javascript
const Helper = require('hubot-test-helper');
const helper = new Helper('../scripts/status_check.js');

expect = require('chai').expect
const expect = require('chai').expect;

describe 'status check', ->
beforeEach ->
@room = helper.createRoom(httpd: false)
describe('status check', function() {
beforeEach(function() {
this.room = helper.createRoom({httpd: false});
});

it 'should send a slack event', ->
response = null
@room.robot.on 'slack.attachment', (event) ->
response = event.content
it('should send a slack event', function() {
let response = null;
this.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!")
this.room.user.say('bob', '@hubot check status').then(() => {
expect(response.text).to.eql("It's all good!");
});
});
});
```

## Development
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{
"name": "hubot-test-helper",
"description": "Helper for testing hubot script",
"main": "./lib/index.js",
"main": "./src/index.js",
"scripts": {
"test": "mocha --compilers coffee:coffee-script/register test",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"prepublish": "coffee --compile --output lib/ src/"
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"keywords": [
"hubot"
],
"dependencies": {
"hubot": ">=2.6.0 <10 || 0.0.0-development"
"hubot": ">=3.0.0 <10 || 0.0.0-development"
},
"devDependencies": {
"chai": "latest",
"co": "latest",
"coffee-script": "latest",
"mocha": "latest",
"coffee-script": "latest",
"semantic-release": "latest"
},
"author": "Fumiaki MATSUSHIMA",
Expand Down

0 comments on commit 6a621be

Please sign in to comment.