Skip to content

Commit

Permalink
feat: find missing files when using all:true option (#208)
Browse files Browse the repository at this point in the history
* add example where a file should not be covered

* find files to include using globby

* insert empty coverage for missed files

* refactor a little

* change the icon for files without any statements

* confirm file not-covered is present in the final report

* search using default extension masks if include is not present
  • Loading branch information
bahmutov authored Apr 21, 2020
1 parent 74b5106 commit 2ce2d90
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 72 deletions.
38 changes: 38 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,43 @@ workflows:
../../node_modules/.bin/only-covered main.js
working_directory: examples/support-files

- cypress/run:
attach-workspace: true
name: example-all-files
requires:
- cypress/install
# there are no jobs to follow this one
# so no need to save the workspace files (saves time)
no-workspace: true
start: npm start --prefix examples/all-files
wait-on: 'http://localhost:1234'
command: npx cypress run --project examples/all-files
# store screenshots and videos
store_artifacts: true
post-steps:
- run: cat examples/all-files/.nyc_output/out.json
- run: cat examples/all-files/coverage/coverage-final.json
# store the created coverage report folder
# you can click on it in the CircleCI UI
# to see live static HTML site
- store_artifacts:
path: examples/all-files/coverage
# make sure the examples captures 100% of code
- run:
command: npx nyc report --check-coverage true --lines 100
working_directory: examples/all-files
- run:
name: Check code coverage 📈
# we will check the final coverage report
# to make sure it only has files we are interested in
# because there are files covered at 0 in the report
command: |
../../node_modules/.bin/check-coverage main.js
../../node_modules/.bin/check-coverage second.js
../../node_modules/.bin/check-coverage not-covered.js
../../node_modules/.bin/only-covered --from coverage/coverage-final.json main.js second.js not-covered.js
working_directory: examples/all-files

- cypress/run:
attach-workspace: true
name: example-exclude-files
Expand Down Expand Up @@ -467,3 +504,4 @@ workflows:
- example-exclude-files
- example-docker-paths
- example-use-webpack
- example-all-files
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ For example, if you want to only include files in the `app` folder, but exclude
}
```

**Note:** if you have `all: true` NYC option set, this plugin will check the produced `.nyc_output/out.json` before generating the final report. If the `out.json` file does not have information for some files that should be there according to `include` list, then an empty placeholder will be included, see [PR 208](https://github.com/cypress-io/code-coverage/pull/208).

## Disable plugin

You can skip the client-side code coverage hooks by setting the environment variable `coverage` to `false`.
Expand Down
37 changes: 37 additions & 0 deletions common-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @ts-check
function combineNycOptions({
pkgNycOptions,
nycrc,
nycrcJson,
defaultNycOptions
}) {
// last option wins
const nycOptions = Object.assign(
{},
defaultNycOptions,
nycrc,
nycrcJson,
pkgNycOptions
)

if (typeof nycOptions.reporter === 'string') {
nycOptions.reporter = [nycOptions.reporter]
}
if (typeof nycOptions.extension === 'string') {
nycOptions.extension = [nycOptions.extension]
}

return nycOptions
}

const defaultNycOptions = {
'report-dir': './coverage',
reporter: ['lcov', 'clover', 'json'],
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
excludeAfterRemap: true
}

module.exports = {
combineNycOptions,
defaultNycOptions
}
2 changes: 1 addition & 1 deletion cypress/integration/combine-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { combineNycOptions, defaultNycOptions } = require('../../task-utils')
const { combineNycOptions, defaultNycOptions } = require('../../common-utils')
describe('Combine NYC options', () => {
it('overrides defaults', () => {
const pkgNycOptions = {
Expand Down
3 changes: 3 additions & 0 deletions examples/all-files/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["istanbul"]
}
1 change: 1 addition & 0 deletions examples/all-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# example: all files
4 changes: 4 additions & 0 deletions examples/all-files/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"fixturesFolder": false,
"baseUrl": "http://localhost:1234"
}
12 changes: 12 additions & 0 deletions examples/all-files/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="cypress" />
it('works', () => {
cy.visit('/')
cy.contains('Page body')

cy.window()
.invoke('reverse', 'super')
.should('equal', 'repus')

// application's code should be instrumented
cy.window().should('have.property', '__coverage__')
})
5 changes: 5 additions & 0 deletions examples/all-files/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = (on, config) => {
require('../../../../task')(on, config)
on('file:preprocessor', require('../../../../use-babelrc'))
return config
}
2 changes: 2 additions & 0 deletions examples/all-files/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '../../../../support'
console.log('this is commands file')
1 change: 1 addition & 0 deletions examples/all-files/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./commands')
17 changes: 17 additions & 0 deletions examples/all-files/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<body>
Page body
<script src="main.js"></script>
<script src="second.js"></script>
<script>
// use functions creates in "main.js"
if (add(2, 3) !== 5) {
throw new Error('wrong addition')
}
if (sub(2, 3) !== -1) {
throw new Error('wrong subtraction')
}
if (reverse('foo') !== 'oof') {
throw new Error('wrong string reverse')
}
</script>
</body>
3 changes: 3 additions & 0 deletions examples/all-files/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.add = (a, b) => a + b

window.sub = (a, b) => a - b
7 changes: 7 additions & 0 deletions examples/all-files/not-covered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// this file is NOT included from "index.html"
// thus it is not instrumented and not included
// in the final code coverage numbers
function throwsError() {
throw new Error('NO')
}
throwsError()
19 changes: 19 additions & 0 deletions examples/all-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "example-all-files",
"description": "Report all files",
"scripts": {
"start": "../../node_modules/.bin/parcel serve index.html",
"cy:open": "../../node_modules/.bin/cypress open",
"cy:run": "../../node_modules/.bin/cypress run",
"dev": "../../node_modules/.bin/start-test 1234 cy:open",
"e2e": "../../node_modules/.bin/start-test 1234 cy:run",
"report": "../../node_modules/.bin/nyc report"
},
"nyc": {
"all": true,
"include": "*.js"
},
"devDependencies": {
"@babel/core": "7.9.0"
}
}
7 changes: 7 additions & 0 deletions examples/all-files/second.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// this file should be excluded from the final coverage numbers
// using "nyc.exclude" list in package.json
window.reverse = s =>
s
.split('')
.reverse()
.join('')
44 changes: 11 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
"@cypress/browserify-preprocessor": "2.2.1",
"debug": "4.1.1",
"execa": "4.0.0",
"nyc": "15.0.1",
"istanbul-lib-coverage": "3.0.0"
"globby": "11.0.0",
"istanbul-lib-coverage": "3.0.0",
"nyc": "15.0.1"
},
"devDependencies": {
"@babel/core": "7.9.0",
Expand Down
Loading

0 comments on commit 2ce2d90

Please sign in to comment.