Mock server that mimics the GitHub API and supports webhooks
See original GitHub issueFeature Request
Is your feature request related to a problem? Please describe.
Simulating a probot environment is hard. You have to mock many things and create fixtures. Also there are many steps when getting started: create a github API, create a private key, use something like smee.io to receive webhooks,…
Describe the solution you’d like I would like to simulate a whole environment locally. Many other platforms do so. If you want to develop an application using Amazon DynamoDB you can download an implementation that you can run locally. Same with Google App Engine: you can download an SDK that simulates the whole environment (or almost).
It would be great to have something similar for octokit/probot. Initially it would just be an HTTP server that mimics the GitHub API. I’ve called it mocktokit
https://github.com/gimenete/mocktokit It stores all the information in memory. I’ll explain the idea better with an example.
const server = await mocktokit(5000, webhook)
// Create an octokit client with a custom baseUrl
const octokit = require('@octokit/rest')({
baseUrl: 'http://localhost:5000'
})
// Now you can start making API requests to the mock server
// Just by using octokit
// At any moment you can get an immutable snapshot of the state
const state1 = server.getState()
// Modify something by using regular octokit methods
await octokit.issues.create({
owner: 'gimenete',
repo: 'test-repo',
title: 'One issue'
})
// Take another snapshot
const state2 = server.getState()
Having something like this would allow anyone to do unit testing against octokit without having to mock anything theirselves, not even have to create fixtures. It would also lower the barrier to create probot applications because it would mimic also the webhooks behavior without having to set up anything else.
The basic structure is there, but it would require to implement all the methods available in octokit one by one. On the flip side, since everything is in memory, I don’t think it would be that hard.
Take a look to the README and let me know what you think.
PS: if this hook mechanism https://github.com/octokit/rest.js/issues/601#issuecomment-401491907 gets implemented, the mock server could be implemented without even creating an http server, just capturing the requests and generating proper responses. The API could be just:
const octokit = require('@octokit/rest')()
const mock = mocktokit(ocktokit) // installs the hook
// At any moment you can get an immutable snapshot of the state
const state1 = mock.getState()
// Modify something by using regular octokit methods
await octokit.issues.create({
owner: 'gimenete',
repo: 'test-repo',
title: 'One issue'
})
// Take another snapshot
const state2 = mock.getState()
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:12 (7 by maintainers)
Top GitHub Comments
Just a quick update. I’ve been able to add the monaco editor and inject the octokit types to it in runtime. Also, I’ve put a log of events to the right with the idea to inspect all events and be able to travel back in time to one of them. There’s also a “copy test to clipboard” which could be a functionality to generate and copy a complete jest/mocha test that re-creates all the events and asserts the final state.
Hawman this looks really cool! That would be useful way beyond Probot, it would be useful to everyone integrating with the GitHub platform 😃