Skip to content

Commit 9f8c4e8

Browse files
committed
Add test coverage in CI
1 parent 4820a55 commit 9f8c4e8

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

.travis.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ script: npm test
1111
# without. We only want to build the one with the tag, because it's the one
1212
# that runs the deployment stage
1313
if: '!(commit_message =~ /^Release/ && tag is blank)'
14-
# Deployment must happen after tests for the whole matrix have been performed,
15-
# so we use `jobs.include` with a different `stage`
1614
jobs:
1715
include:
16+
# Test coverage check must happen after tests for the whole matrix have
17+
# been performed, so we get the merged coverage map
18+
- stage: Coverage check
19+
os: linux
20+
script: npm run check-coverage
21+
# If CI fails, codecov does not post a bot comment on PR, so we skip it
22+
if: type != pull_request && branch == master
23+
# Deployment must happen after tests for the whole matrix have been
24+
# performed, so we use `jobs.include` with a different `stage`
1825
- stage: Deploy
1926
os: linux
2027
# We have already run tests

gulp/tasks/coverage.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
3+
const {
4+
env: { TRAVIS_REPO_SLUG, TRAVIS_COMMIT },
5+
} = require('process')
6+
7+
const isCi = require('is-ci')
8+
const fetch = require('cross-fetch')
9+
const PluginError = require('plugin-error')
10+
11+
// In CI, once each environment has sent their test coverage maps, we check that
12+
// when merging them we are above the minimum threshold
13+
const checkCoverage = async function() {
14+
if (!isCi) {
15+
return
16+
}
17+
18+
const coverage = await getCoverage()
19+
20+
if (coverage < COVERAGE_THRESHOLD) {
21+
throw new PluginError(
22+
'gulp-codecov-check',
23+
`Test coverage is ${coverage}% but should be at least ${COVERAGE_THRESHOLD}%`,
24+
)
25+
}
26+
}
27+
28+
const getCoverage = async function() {
29+
const codecovUrl = `https://codecov.io/api/gh/${TRAVIS_REPO_SLUG}/commit/${TRAVIS_COMMIT}`
30+
const response = await fetch(codecovUrl)
31+
const {
32+
commit: {
33+
// eslint-disable-next-line id-length
34+
totals: { c: coverage },
35+
},
36+
} = await response.json()
37+
38+
const coverageA = Number(coverage)
39+
return coverageA
40+
}
41+
42+
const COVERAGE_THRESHOLD = 100
43+
44+
module.exports = {
45+
checkCoverage,
46+
}

gulp/tasks/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
...require('./test'),
66
...require('./check'),
77
...require('./unit'),
8+
...require('./coverage'),
89
...require('./build'),
910
...require('./emit'),
1011
}

package-lock.json

Lines changed: 33 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"scripts": {
1111
"build": "gulp build",
1212
"test": "npm run build && gulp test",
13-
"release": "npm test && release-it"
13+
"release": "npm test && release-it",
14+
"check-coverage": "gulp checkCoverage"
1415
},
1516
"husky": {
1617
"hooks": {
@@ -61,6 +62,7 @@
6162
"@babel/plugin-transform-runtime": "^7.2.0",
6263
"@babel/preset-env": "^7.2.0",
6364
"ava": "^1.0.0-rc.2",
65+
"cross-fetch": "^2.2.3",
6466
"eslint": "^5.9.0",
6567
"eslint-config-prettier": "^3.3.0",
6668
"eslint-config-standard": "^12.0.0",

0 commit comments

Comments
 (0)