Skip to content

Commit

Permalink
Raise errors in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Jun 30, 2017
1 parent b4d946f commit 1cc4505
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
14 changes: 10 additions & 4 deletions lib/robot.js
Expand Up @@ -9,16 +9,18 @@ const Context = require('./context');
* @property {logger} log - A logger
*/
class Robot {
constructor({app, cache, logger} = {}) {
constructor({app, cache, logger, throwErrors} = {}) {
this.events = new EventEmitter();
this.app = app;
this.cache = cache;
this.log = wrapLogger(logger);
this.throwErrors = throwErrors;
}

async receive(event) {
await this.events.emit('*', event);
await this.events.emit(event.event, event);
return this.events.emit('*', event).then(() => {
return this.events.emit(event.event, event);
});
}

/**
Expand Down Expand Up @@ -64,7 +66,11 @@ class Robot {
const context = new Context(event, github);
return callback(context, context /* DEPRECATED: for backward compat */);
} catch (err) {
this.log.error(err);
if (this.throwErrors) {
throw err;
} else {
this.log.error(err);
}
}
}
});
Expand Down
33 changes: 18 additions & 15 deletions test/index.js
@@ -1,13 +1,13 @@
const expect = require('expect');
const createProbot = require('..');
const {createRobot} = require('..');

describe('Probot', () => {
let probot;
describe('Robot', () => {
let robot;
let event;

beforeEach(() => {
probot = createProbot();
probot.robot.auth = () => Promise.resolve({});
robot = createRobot({throwErrors: true});
robot.auth = () => Promise.resolve({});

event = {
event: 'push',
Expand All @@ -18,21 +18,24 @@ describe('Probot', () => {
describe('receive', () => {
it('delivers the event', async () => {
const spy = expect.createSpy();
probot.load(robot => robot.on('push', spy));
robot.on('push', spy);

await probot.receive(event);
await robot.receive(event);

expect(spy).toHaveBeenCalled();
});

it('raises errors thrown in plugins', async () => {
probot.load(robot => robot.on('push', () => {
throw new Error('something happened');
}));

expect(async () => {
await probot.receive(event);
}).toThrow();
it('returns a reject errors thrown in plugins', async () => {
robot.on('push', () => {
throw new Error('error from plugin');
});

try {
await robot.receive(event);
throw new Error('expected error to be raised from plugin');
} catch (err) {
expect(err.message).toEqual('error from plugin');
}
});
});
});
2 changes: 1 addition & 1 deletion test/robot.js
Expand Up @@ -82,7 +82,7 @@ describe('Robot', function () {
spy();
resolve();
}, 1);
})
});
});

await robot.receive(event);
Expand Down

0 comments on commit 1cc4505

Please sign in to comment.