probot/docs/testing.md

53 lines
2.3 KiB
Markdown
Raw Normal View History

2017-08-18 20:46:23 +08:00
---
next: docs/pagination.md
---
# Testing
2017-10-24 10:50:47 +08:00
We highly recommend working in the style of [test-driven development](http://agiledata.org/essays/tdd.html) when creating probot apps. It is frustrating to constantly create real GitHub events in order to test an app. Redelivering webhooks is possible and can be accessed in your app's [settings](https://github.com/settings/apps) page under the **Advanced** tab. We do offer the above documented `simulate` method to help make this easier; however, by writing your tests first, you can avoid repeatedly recreating actual events from GitHub to check if your code is working.
2017-08-18 20:46:23 +08:00
For our testing examples, we use [jest](https://facebook.github.io/jest/), but there are other options that can perform similar operations. Here's an example of creating a robot instance and mocking out the GitHub API:
2017-08-18 20:46:23 +08:00
```js
// Requiring probot allows us to mock out a robot instance
const {createRobot} = require('probot')
2017-10-24 10:50:47 +08:00
// Requiring our app
const app = require('')
2017-08-18 20:46:23 +08:00
// Create a fixtures folder in your test folder
// Then put any larger testing payloads in there
const payload = require('./fixtures/payload')
2017-08-18 20:46:23 +08:00
2017-08-20 23:42:51 +08:00
describe('your-app', () => {
let robot
let github
2017-08-18 20:46:23 +08:00
beforeEach(() => {
// Here we create a robot instance
robot = createRobot()
2017-08-20 23:42:51 +08:00
// Here we initialize the app on the robot instance
app(robot)
2017-08-18 20:46:23 +08:00
// This is an easy way to mock out the GitHub API
github = {
issues: {
createComment: jest.fn().mockReturnValue(Promise.resolve({
2017-08-18 20:46:23 +08:00
// Whatever the GitHub API should return
}))
}
}
// Passes the mocked out GitHub API into out robot instance
robot.auth = () => Promise.resolve(github)
})
2017-08-18 20:46:23 +08:00
describe('your functionality', () => {
it('performs an action', async () => {
// Simulates delivery of a payload
await robot.receive(payload)
2017-08-18 20:46:23 +08:00
// This test would pass if in your main code you called `context.github.issues.createComment`
expect(github.issues.createComment).toHaveBeenCalled()
})
})
})
2017-08-18 20:46:23 +08:00
```
2017-08-19 03:50:15 +08:00
A good testing example from [update-docs](https://github.com/behaviorbot/update-docs) can be found [here](https://github.com/behaviorbot/update-docs/blob/master/test/index.js), and another one from [owners](https://github.com/probot/owners) can be found [here](https://github.com/probot/owners/blob/master/test/owner-notifier.js).