Skip to content

Commit

Permalink
test: refactor index.js to achieve 100% test coverage (#13)
Browse files Browse the repository at this point in the history
* achieve 100% test coverage

This seeks to establish 100% test coverage by homing `run`'s testable
logic in `run.js` and making `index.js` a logic-less invocation of
`run`.

```
-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |      100 |     100 |     100 |
 github.js |     100 |      100 |     100 |     100 |
 run.js    |     100 |      100 |     100 |     100 |
 utils.js  |     100 |      100 |     100 |     100 |
-----------|---------|----------|---------|---------|-------------------

Test Suites: 3 passed, 3 total
Tests:       21 passed, 21 total
Snapshots:   0 total
Time:        0.855 s, estimated 1 s
Ran all test suites.
✨  Done in 1.71s.
```

Signed-off-by: Mike Ball <[email protected]>

* pr 13 patch

* fix await issue

* update dist build

---------

Signed-off-by: Mike Ball <[email protected]>
Co-authored-by: John Veldboom <[email protected]>
  • Loading branch information
mdb and jveldboom authored Feb 13, 2023
1 parent 912fbb5 commit 768da03
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 54 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ yarn lint
- [x] List action in marketplace
- [ ] Add version suffix that are semver
- [ ] Improve integration testing to cover all use-case. May require the ability to pass in a list of commits
- [ ] Improve index.js file
- [ ] Improve run.js file
- Should it be simplified and wrapped in a try/catch?
- How can we get 100% test coverage on it?

## Notes
- Commit Analyzer https://github.com/semantic-release/commit-analyzer#releaserules
Expand Down
32 changes: 19 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44138,15 +44138,15 @@ module.exports = {

/***/ }),

/***/ 4351:
/***/ 2475:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const semver = __nccwpck_require__(1383)
const core = __nccwpck_require__(2186)
const github = __nccwpck_require__(8396)
const utils = __nccwpck_require__(1608)

const run = async () => {
module.exports = async () => {
const octokit = github.getOctokit(core.getInput('github-token'))

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
Expand Down Expand Up @@ -44177,10 +44177,6 @@ const run = async () => {
utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

if (process.env.NODE_ENV !== 'test') run()

module.exports = { run }


/***/ }),

Expand Down Expand Up @@ -44455,12 +44451,22 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __nccwpck_require__(4351);
/******/ module.exports = __webpack_exports__;
/******/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const core = __nccwpck_require__(2186)
const run = __nccwpck_require__(2475); // semicolon is intentional for standardjs

(async () => {
try {
await run()
} catch (err) {
core.setFailed(err.message)
}
})()

})();

module.exports = __webpack_exports__;
/******/ })()
;
39 changes: 5 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,10 @@
const semver = require('semver')
const core = require('@actions/core')
const github = require('./github')
const utils = require('./utils')
const run = require('./run'); // semicolon is intentional for standardjs

const run = async () => {
const octokit = github.getOctokit(core.getInput('github-token'))

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
const sha = process.env.GITHUB_SHA

let latestTag
(async () => {
try {
latestTag = await github.getLatestTag(octokit, owner, repo)
await run()
} catch (err) {
return core.setFailed(`unable to get latest tag - error: ${err.message} ${err?.response?.status}`)
}

// return a default version if no previous github tags
if (!latestTag) {
const incrementedVersion = semver.inc('0.0.0', core.getInput('default-bump'))
return utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

if (!semver.valid(latestTag.name)) {
return core.setFailed(`latest tag name is not valid semver: ${JSON.stringify(latestTag)}`)
core.setFailed(err.message)
}

// get commits from last tag and calculate version bump
const commits = await github.compareCommits(octokit, owner, repo, latestTag?.commit?.sha, sha)
const bump = await utils.getVersionBump(commits, core.getInput('default-bump'))

const incrementedVersion = semver.inc(latestTag.name, bump)
utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

if (process.env.NODE_ENV !== 'test') run()

module.exports = { run }
})()
35 changes: 35 additions & 0 deletions src/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const semver = require('semver')
const core = require('@actions/core')
const github = require('./github')
const utils = require('./utils')

module.exports = async () => {
const octokit = github.getOctokit(core.getInput('github-token'))

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
const sha = process.env.GITHUB_SHA

let latestTag
try {
latestTag = await github.getLatestTag(octokit, owner, repo)
} catch (err) {
return core.setFailed(`unable to get latest tag - error: ${err.message} ${err?.response?.status}`)
}

// return a default version if no previous github tags
if (!latestTag) {
const incrementedVersion = semver.inc('0.0.0', core.getInput('default-bump'))
return utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

if (!semver.valid(latestTag.name)) {
return core.setFailed(`latest tag name is not valid semver: ${JSON.stringify(latestTag)}`)
}

// get commits from last tag and calculate version bump
const commits = await github.compareCommits(octokit, owner, repo, latestTag?.commit?.sha, sha)
const bump = await utils.getVersionBump(commits, core.getInput('default-bump'))

const incrementedVersion = semver.inc(latestTag.name, bump)
utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}
10 changes: 5 additions & 5 deletions src/index.test.js → src/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.spyOn(core, 'getInput')
jest.spyOn(core, 'setFailed')
jest.spyOn(core, 'setOutput')

const index = require('./index')
const run = require('./run')

describe('index', () => {
beforeEach(() => {
Expand All @@ -24,14 +24,14 @@ describe('index', () => {
it('should fail when unable to get latest tag', async () => {
github.getLatestTag.mockRejectedValueOnce(new Error('test error'))

await index.run()
await run()
expect(core.setFailed).toBeCalledTimes(1)
})

it('should output default version bump (0.0.1) if no previous tags', async () => {
github.getLatestTag.mockResolvedValueOnce()

await index.run()
await run()
expect(core.getInput).toHaveBeenNthCalledWith(2, 'default-bump')
expect(core.getInput).toHaveNthReturnedWith(2, 'patch')

Expand All @@ -46,7 +46,7 @@ describe('index', () => {
it('should fail when latest tag is no valid semver', async () => {
github.getLatestTag.mockResolvedValueOnce('invalid-semver')

await index.run()
await run()
expect(core.setFailed).toBeCalledTimes(1)
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'latest tag name is not valid semver: "invalid-semver"')
})
Expand All @@ -59,7 +59,7 @@ describe('index', () => {
github.getLatestTag.mockResolvedValueOnce(latestTag)
github.compareCommits.mockResolvedValueOnce([])

await index.run()
await run()

expect(core.setOutput).toHaveBeenNthCalledWith(1, 'version', '1.2.4')
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'version-with-prefix', 'v1.2.4')
Expand Down

0 comments on commit 768da03

Please sign in to comment.