How to simulate a fake PR? #3
-
|
My action queries the last PR with I have read the documentation several times and looked at many examples which I found through the github search. I can't figure out, how can I create a fake PR to use it with the tests. Is it possible to simulate or create a fake pull request before the action test runs? I created a gist with the code I have tried. If you have any suggestions I would be pleased. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Hi! Looked quickly at your gist, I believe you chose the right approach with mocking API. Creating and then deleting a temporary PR using API can be also an option. Will look at it during the week. Can you compose your gist to a repo so that I can test it locally? |
Beta Was this translation helpful? Give feedback.
-
|
Hi! Is it still relevant? I came with 2 suggestions regarding your need. FirstYou can manually create a test PR, look at its ID and put this ID into options githubContext: options.setGithubContext({ payload: { pull_request: { number: YOUR_HARDCODED_ID } } })This way you don't need to mock API. You can even use a separate repo if you don't like bloating your main repo. In this case use options.setGithubContext({
payload: { pull_request: { number: YOUR_HARDCODED_ID } },
repository: 'owner/repo'
})SecondYou can extract the code of your action into a function and call it passing API client: export async function runAction(apiClient) {
...
}
runAction(new Octokit({ auth: token })And use Single Function Target to run the function in an emulated Action environment. This way you can mock ApiClient by normal jest mocks: import {runAction} from 'main.js'
const apiMock = createJestMock()
const target = RunTarget.asyncFn(
async () => runAction(apiMock),
'path/to/action.yml'
);
const result = target.run(RunOptions.create( ... )) |
Beta Was this translation helpful? Give feedback.
Hi! Is it still relevant?
I came with 2 suggestions regarding your need.
First
You can manually create a test PR, look at its ID and put this ID into options githubContext:
This way you don't need to mock API.
You can even use a separate repo if you don't like bloating your main repo. In this case use
Second
You can extract the code of your action into a function and call it passing API client: