Skip to content

Commit 1f3e7aa

Browse files
authored
Merge pull request #45 from hubotio/upgrade-to-hubot-7
feat: Bump Hubot version to 7 and update README doc with working exam…
2 parents abc8050 + 82c5a8b commit 1f3e7aa

File tree

5 files changed

+5528
-600
lines changed

5 files changed

+5528
-600
lines changed

README.md

Lines changed: 24 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# mock adapter for unit-testing Hubot
1+
# Mock adapter for unit-testing Hubot
22

33
I've whacked together a couple of Hubot scripts, but then they started getting
44
more complicated. TDD is really the ONLY way to do any kind of meaningful
@@ -9,7 +9,7 @@ I couldn't find an existing method for writing unit tests for Hubot scripts.
99
After digging around under Hubot's hood, I figured out all I really needed was
1010
an `Adapter` implementation I could spy on. That is what you see here.
1111

12-
## example usage
12+
## Example Usage
1313

1414
Let's assume you've got a really simple script, like this:
1515

@@ -21,96 +21,44 @@ module.exports = function (robot) {
2121
};
2222
```
2323

24-
You want to test this, of course. So create a Mocha test:
24+
### Node Test Runner
2525

26-
```js
27-
var expect = require("chai").expect;
28-
var path = require("path");
29-
30-
var Robot = require("hubot/es2015").Robot;
31-
var TextMessage = require("hubot").TextMessage;
32-
33-
describe("Eddie the shipboard computer", function () {
34-
var robot;
35-
var user;
36-
var adapter;
37-
38-
beforeEach((done) => {
39-
// create new robot, without http, using the mock adapter
40-
robot = new Robot("hubot-mock-adapter", false, "Eddie");
41-
42-
// start adapter
43-
robot.loadAdapter().then(() => {
44-
// only load scripts we absolutely need, like auth.coffee
45-
process.env.HUBOT_AUTH_ADMIN = "1";
46-
robot.loadFile(
47-
path.resolve(path.join("node_modules/hubot/src/scripts")),
48-
"auth.coffee"
49-
);
50-
51-
// load the module under test and configure it for the
52-
// robot. This is in place of external-scripts
53-
require("../index")(robot);
54-
55-
robot.adapter.on("connected", () => {
56-
// create a user
57-
user = robot.brain.userForId("1", {
58-
name: "mocha",
59-
room: "#mocha",
60-
});
61-
adapter = robot.adapter;
62-
done();
63-
});
64-
65-
// start the bot
66-
robot.run();
67-
});
68-
});
69-
70-
afterEach(function () {
71-
robot.shutdown();
72-
});
26+
Create a test file in a folder. e.g. [index.test.mjs](test/index.test.mjs).
7327

74-
it("responds when greeted", function (done) {
75-
// here's where the magic happens!
76-
adapter.on("reply", function (envelope, strings) {
77-
expect(strings[0]).match(/Why hello there/);
28+
You'll need a Node version > 17.
7829

79-
done();
80-
});
30+
Assuming your tests are in `test/` or have `.test.` before the file extension, just run `node --test`. For less typing, in your `package.json`, add a `test` script:
8131

82-
adapter.receive(new TextMessage(user, "Computer!"));
83-
});
84-
});
32+
```json
33+
"scripts": {
34+
"test": "node --test"
35+
}
8536
```
8637

38+
Then you can use `npm test` to run your tests!
39+
40+
### Mocha
41+
42+
Create a test file in a folder. e.g. [index.js](test/index.js).
43+
8744
You'll need `devDependencies` something like this in your `package.json`:
8845

8946
```js
9047
"devDependencies": {
91-
"coffee-script": "~1.6.3",
92-
"chai": "~1.9.0",
93-
"hubot-mock-adapter": "~1.0.0",
94-
"mocha": "~1.17.1",
95-
"hubot": "~2.7.2",
96-
"sinon": "~1.9.0"
48+
"coffeescript": "^2.7.0",
49+
"chai": "^4.3.7",
50+
"hubot-mock-adapter": "^1.x.x",
51+
"mocha": "^10.2.0",
52+
"hubot": "^7.x.x",
9753
}
9854
```
9955

100-
That's (almost) all there is to it!
101-
102-
## firing up Mocha
56+
Assuming your tests are in `test/`, just run `node_modules/.bin/mocha --exit`. For less typing, in your `package.json`, add a `test` script:
10357

104-
Assuming you're using [`mocha`][mocha] to run your tests, and your
105-
tests are in `test/`, just run `node_modules/.bin/mocha --compilers coffee:coffee-script`.
106-
For less typing, in your `package.json`, add a `test` script:
107-
108-
```js
58+
```json
10959
"scripts": {
110-
"test": "mocha --compilers coffee:coffee-script"
60+
"test": "mocha --exit"
11161
}
11262
```
11363

11464
Then you can use `npm test` to run your tests!
115-
116-
[mocha]: https://github.com/mhevery/jasmine-node

0 commit comments

Comments
 (0)