Skip to content

Commit

Permalink
feature/add test (#8)
Browse files Browse the repository at this point in the history
feat: added test code
  • Loading branch information
bonyuta0204 authored Nov 30, 2023
1 parent 194b027 commit 5ead745
Show file tree
Hide file tree
Showing 6 changed files with 1,007 additions and 88 deletions.
51 changes: 51 additions & 0 deletions __tests__/run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { setFailed } from '@actions/core'
import { run } from '../src/run'
import { fetchRemoteBranches } from '../src/git-util'
import { vi, expect, describe, it, beforeEach } from 'vitest'

vi.mock('@actions/core', () => ({
getInput: vi.fn(),
setFailed: vi.fn(),
info: vi.fn()
}))

vi.mock('../src/git-util', () => ({
fetchRemoteBranches: vi.fn(),
hasCommitsBetween: vi.fn()
}))

describe('main function tests', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('should fail if source or target branch is not specified', async () => {
await run({
srcBranch: '',
targetBranch: 'target-branch',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner'
})

expect(setFailed).toHaveBeenCalledWith(
'Source or target branch not specified'
)
})

it('should fail if source branch does not exist', async () => {
;(fetchRemoteBranches as any).mockResolvedValue(['valid-branch'])

await run({
srcBranch: 'nonexistent-branch',
targetBranch: 'valid-branch',
repoToken: 'dummy-token',
repo: 'test-repo',
owner: 'test-owner'
})

expect(setFailed).toHaveBeenCalledWith(
'Source branch nonexistent-branch does not exist'
)
})
})
25 changes: 17 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36351,17 +36351,22 @@ var __webpack_exports__ = {};
var exports = __webpack_exports__;

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.run = void 0;
const core_1 = __nccwpck_require__(9093);
const github_1 = __nccwpck_require__(5942);
const git_util_1 = __nccwpck_require__(8561);
async function main() {
const token = (0, core_1.getInput)('repo-token');
const octokit = (0, github_1.getOctokit)(token);
const srcBranch = (0, core_1.getInput)('src-branch');
const targetBranch = (0, core_1.getInput)('target-branch');
const title = (0, core_1.getInput)('title');
const body = (0, core_1.getInput)('body');
const { repo, owner } = github_1.context.repo;
const generateOptionParams = () => ({
srcBranch: (0, core_1.getInput)('src-branch'),
targetBranch: (0, core_1.getInput)('target-branch'),
title: (0, core_1.getInput)('title'),
body: (0, core_1.getInput)('body'),
repoToken: (0, core_1.getInput)('repo-token'),
repo: github_1.context.repo.repo,
owner: github_1.context.repo.owner
});
async function run(options) {
const { srcBranch, targetBranch, title, body, repoToken, repo, owner } = options;
const octokit = (0, github_1.getOctokit)(repoToken);
if (!srcBranch || !targetBranch) {
(0, core_1.setFailed)('Source or target branch not specified');
return;
Expand Down Expand Up @@ -36416,6 +36421,10 @@ async function main() {
(0, core_1.setFailed)(`Error creating pull request: ${error.message}`);
});
}
exports.run = run;
async function main() {
run(generateOptionParams());
}
main();

})();
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"format:write": "prettier --write **/*.ts",
"format:check": "prettier --check **/*.ts",
"package": "ncc build src/index.ts --license licenses.txt",
"package:watch": "npm run package -- --watch"
"package:watch": "npm run package -- --watch",
"test": "vitest test"
},
"keywords": [],
"author": "",
Expand All @@ -22,7 +23,8 @@
"@types/node": "^20.10.0",
"@vercel/ncc": "^0.38.1",
"prettier": "^3.1.0",
"typescript": "^5.3.2"
"typescript": "^5.3.2",
"vitest": "1.0.0-beta.6"
},
"volta": {
"node": "20.10.0"
Expand Down
Loading

0 comments on commit 5ead745

Please sign in to comment.