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

File tree

20 files changed

+292
-72
lines changed

20 files changed

+292
-72
lines changed

.circleci/config.yml

Lines changed: 38 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

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

examples/all-files/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# example: all files

examples/all-files/cypress.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fixturesFolder": false,
3+
"baseUrl": "http://localhost:1234"
4+
}
Lines changed: 12 additions & 0 deletions
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+
})
Lines changed: 5 additions & 0 deletions
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+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '../../../../support'
2+
console.log('this is commands file')

0 commit comments

Comments
 (0)