Skip to content

Commit 2ce2d90

Browse files
authored
feat: find missing files when using all:true option (#208)
* 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
1 parent 74b5106 commit 2ce2d90

20 files changed

+292
-72
lines changed

.circleci/config.yml

+38
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,43 @@ workflows:
354354
../../node_modules/.bin/only-covered main.js
355355
working_directory: examples/support-files
356356

357+
- cypress/run:
358+
attach-workspace: true
359+
name: example-all-files
360+
requires:
361+
- cypress/install
362+
# there are no jobs to follow this one
363+
# so no need to save the workspace files (saves time)
364+
no-workspace: true
365+
start: npm start --prefix examples/all-files
366+
wait-on: 'http://localhost:1234'
367+
command: npx cypress run --project examples/all-files
368+
# store screenshots and videos
369+
store_artifacts: true
370+
post-steps:
371+
- run: cat examples/all-files/.nyc_output/out.json
372+
- run: cat examples/all-files/coverage/coverage-final.json
373+
# store the created coverage report folder
374+
# you can click on it in the CircleCI UI
375+
# to see live static HTML site
376+
- store_artifacts:
377+
path: examples/all-files/coverage
378+
# make sure the examples captures 100% of code
379+
- run:
380+
command: npx nyc report --check-coverage true --lines 100
381+
working_directory: examples/all-files
382+
- run:
383+
name: Check code coverage 📈
384+
# we will check the final coverage report
385+
# to make sure it only has files we are interested in
386+
# because there are files covered at 0 in the report
387+
command: |
388+
../../node_modules/.bin/check-coverage main.js
389+
../../node_modules/.bin/check-coverage second.js
390+
../../node_modules/.bin/check-coverage not-covered.js
391+
../../node_modules/.bin/only-covered --from coverage/coverage-final.json main.js second.js not-covered.js
392+
working_directory: examples/all-files
393+
357394
- cypress/run:
358395
attach-workspace: true
359396
name: example-exclude-files
@@ -467,3 +504,4 @@ workflows:
467504
- example-exclude-files
468505
- example-docker-paths
469506
- example-use-webpack
507+
- example-all-files

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ For example, if you want to only include files in the `app` folder, but exclude
298298
}
299299
```
300300

301+
**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).
302+
301303
## Disable plugin
302304

303305
You can skip the client-side code coverage hooks by setting the environment variable `coverage` to `false`.

common-utils.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @ts-check
2+
function combineNycOptions({
3+
pkgNycOptions,
4+
nycrc,
5+
nycrcJson,
6+
defaultNycOptions
7+
}) {
8+
// last option wins
9+
const nycOptions = Object.assign(
10+
{},
11+
defaultNycOptions,
12+
nycrc,
13+
nycrcJson,
14+
pkgNycOptions
15+
)
16+
17+
if (typeof nycOptions.reporter === 'string') {
18+
nycOptions.reporter = [nycOptions.reporter]
19+
}
20+
if (typeof nycOptions.extension === 'string') {
21+
nycOptions.extension = [nycOptions.extension]
22+
}
23+
24+
return nycOptions
25+
}
26+
27+
const defaultNycOptions = {
28+
'report-dir': './coverage',
29+
reporter: ['lcov', 'clover', 'json'],
30+
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
31+
excludeAfterRemap: true
32+
}
33+
34+
module.exports = {
35+
combineNycOptions,
36+
defaultNycOptions
37+
}

cypress/integration/combine-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { combineNycOptions, defaultNycOptions } = require('../../task-utils')
1+
const { combineNycOptions, defaultNycOptions } = require('../../common-utils')
22
describe('Combine NYC options', () => {
33
it('overrides defaults', () => {
44
const pkgNycOptions = {

examples/all-files/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["istanbul"]
3+
}

examples/all-files/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# example: all files

examples/all-files/cypress.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fixturesFolder": false,
3+
"baseUrl": "http://localhost:1234"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
it('works', () => {
3+
cy.visit('/')
4+
cy.contains('Page body')
5+
6+
cy.window()
7+
.invoke('reverse', 'super')
8+
.should('equal', 'repus')
9+
10+
// application's code should be instrumented
11+
cy.window().should('have.property', '__coverage__')
12+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = (on, config) => {
2+
require('../../../../task')(on, config)
3+
on('file:preprocessor', require('../../../../use-babelrc'))
4+
return config
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '../../../../support'
2+
console.log('this is commands file')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./commands')

examples/all-files/index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<body>
2+
Page body
3+
<script src="main.js"></script>
4+
<script src="second.js"></script>
5+
<script>
6+
// use functions creates in "main.js"
7+
if (add(2, 3) !== 5) {
8+
throw new Error('wrong addition')
9+
}
10+
if (sub(2, 3) !== -1) {
11+
throw new Error('wrong subtraction')
12+
}
13+
if (reverse('foo') !== 'oof') {
14+
throw new Error('wrong string reverse')
15+
}
16+
</script>
17+
</body>

examples/all-files/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.add = (a, b) => a + b
2+
3+
window.sub = (a, b) => a - b

examples/all-files/not-covered.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// this file is NOT included from "index.html"
2+
// thus it is not instrumented and not included
3+
// in the final code coverage numbers
4+
function throwsError() {
5+
throw new Error('NO')
6+
}
7+
throwsError()

examples/all-files/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "example-all-files",
3+
"description": "Report all files",
4+
"scripts": {
5+
"start": "../../node_modules/.bin/parcel serve index.html",
6+
"cy:open": "../../node_modules/.bin/cypress open",
7+
"cy:run": "../../node_modules/.bin/cypress run",
8+
"dev": "../../node_modules/.bin/start-test 1234 cy:open",
9+
"e2e": "../../node_modules/.bin/start-test 1234 cy:run",
10+
"report": "../../node_modules/.bin/nyc report"
11+
},
12+
"nyc": {
13+
"all": true,
14+
"include": "*.js"
15+
},
16+
"devDependencies": {
17+
"@babel/core": "7.9.0"
18+
}
19+
}

examples/all-files/second.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// this file should be excluded from the final coverage numbers
2+
// using "nyc.exclude" list in package.json
3+
window.reverse = s =>
4+
s
5+
.split('')
6+
.reverse()
7+
.join('')

package-lock.json

+11-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@
5050
"@cypress/browserify-preprocessor": "2.2.1",
5151
"debug": "4.1.1",
5252
"execa": "4.0.0",
53-
"nyc": "15.0.1",
54-
"istanbul-lib-coverage": "3.0.0"
53+
"globby": "11.0.0",
54+
"istanbul-lib-coverage": "3.0.0",
55+
"nyc": "15.0.1"
5556
},
5657
"devDependencies": {
5758
"@babel/core": "7.9.0",

0 commit comments

Comments
 (0)