-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.js
63 lines (56 loc) · 1.98 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// @ts-check
require('console.table')
const { getNycReportFilename } = require('./task-utils')
const { existsSync } = require('fs')
const NYC = require('nyc')
const debug = require('debug')('code-coverage')
const { reportCodeCoverageGHACallback } = require('./src/utils')
const { isPluginDisabled } = require('./common-utils')
const nycFilename = getNycReportFilename(process.cwd())
function registerCodeCoveragePlugin(on, config) {
require('./task')(on, config)
// the user can report the code coverage after each spec
// reportAfterEachSpec: false = disabled reporting
// reportAfterEachSpec: true = enabled reporting, default reporter
// reportAfterEachSpec: 'text' = enabled "text" code coverage reporter
// typical values: 'text-summary', 'text'
let reportAfterEachSpec = 'text-summary'
let shouldReport = !isPluginDisabled(config.env)
if (
config.env &&
typeof config.env.coverage === 'object' &&
'reportAfterEachSpec' in config.env.coverage
) {
if (config.env.coverage.reportAfterEachSpec === false) {
shouldReport = false
} else if (config.env.coverage.reportAfterEachSpec === true) {
// use the default code coverage reporter
} else {
reportAfterEachSpec = config.env.coverage.reportAfterEachSpec
debug('')
}
}
debug('should report? %s', shouldReport)
if (shouldReport) {
const nyc = new NYC({
cwd: process.cwd(),
reporter: reportAfterEachSpec,
})
on('after:spec', (t) => {
console.log('code coverage after spec %s', t.relative)
if (existsSync(nycFilename)) {
return nyc.report()
} else {
console.warn('Could not find coverage file %s', nycFilename)
}
})
if (process.env.GITHUB_ACTIONS) {
debug('will report code coverage on GitHub Actions')
on('after:run', reportCodeCoverageGHACallback)
}
}
// IMPORTANT to return the config object
// with the any changed environment variables
return config
}
module.exports = registerCodeCoveragePlugin