Skip to content

Commit

Permalink
feat: allow explicit disable (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov authored Jun 2, 2023
1 parent a899ceb commit 1d40f28
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,18 @@ or set it to `false` in the `cypress.json` file
}
```

If you have an object with coverage settings, you can still disable it

```json
{
"env": {
"coverage": {
"disable": true
}
}
}
```

See [Cypress environment variables](https://on.cypress.io/environment-variables) and [support.js](support.js). You can try running without code coverage in this project yourself

```shell
Expand Down
17 changes: 17 additions & 0 deletions common-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,26 @@ const removePlaceholders = (coverage) => {
})
}

/**
* Returns true if the user disabled the plugin using the env object.
*/
function isPluginDisabled(cyEnv) {
if (cyEnv.coverage === false) {
return true
}
if (typeof cyEnv.coverage === 'object') {
// the user explicitly disabled
return cyEnv.coverage.disabled === true
}

// by default the plugin is enabled
return false
}

module.exports = {
combineNycOptions,
defaultNycOptions,
fileCoveragePlaceholder,
removePlaceholders,
isPluginDisabled,
}
3 changes: 2 additions & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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())

Expand All @@ -17,7 +18,7 @@ function registerCodeCoveragePlugin(on, config) {
// reportAfterEachSpec: 'text' = enabled "text" code coverage reporter
// typical values: 'text-summary', 'text'
let reportAfterEachSpec = 'text-summary'
let shouldReport = true
let shouldReport = !isPluginDisabled(config.env)
if (
config.env &&
typeof config.env.coverage === 'object' &&
Expand Down
5 changes: 4 additions & 1 deletion support.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
excludeByUser,
filterSupportFilesFromCoverage,
} = require('./support-utils')
const { isPluginDisabled } = require('./common-utils')

dayjs.extend(duration)

Expand Down Expand Up @@ -300,7 +301,9 @@ const cyEnvs = Cypress._.mapKeys(Cypress.env(), (value, key) =>
key.toLowerCase(),
)

if (cyEnvs.coverage === false) {
const pluginDisabled = isPluginDisabled(cyEnvs)

if (pluginDisabled) {
console.log('Skipping code coverage hooks')
} else if (Cypress.env('codeCoverageTasksRegistered') !== true) {
// register a hook just to log a message
Expand Down

0 comments on commit 1d40f28

Please sign in to comment.