Skip to content

Commit

Permalink
fix: do not try to generate NYC report if there is no NYC output JSON…
Browse files Browse the repository at this point in the history
… file (#4)

* refactoring and adding example without code coverage report

* fix: do not generate report without nyc file

* run no-files example on CircleCI
  • Loading branch information
bahmutov authored Mar 15, 2022
1 parent 5531e79 commit c0424bd
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 29 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,19 @@ workflows:
../../node_modules/.bin/only-covered first.js second.js third.js
working_directory: examples/multiple-files

- cypress/run:
attach-workspace: true
name: example-no-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
working_directory: examples/no-files
start: npm start
wait-on: 'http://localhost:1234'
command: '../../node_modules/.bin/cypress run'

# - cypress/run:
# attach-workspace: true
# name: example-use-webpack
Expand Down
3 changes: 3 additions & 0 deletions examples/no-files/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["istanbul"]
}
3 changes: 3 additions & 0 deletions examples/no-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# example: no-files

All tests are skipped, no code coverage
11 changes: 11 additions & 0 deletions examples/no-files/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"fixturesFolder": false,
"pluginsFile": "cypress/plugins/index.js",
"baseUrl": "http://localhost:1234",
"video": false,
"env": {
"coverage": {
"exclude": true
}
}
}
5 changes: 5 additions & 0 deletions examples/no-files/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="cypress" />
it.skip('works', () => {
cy.visit('/')
cy.contains('Page body')
})
6 changes: 6 additions & 0 deletions examples/no-files/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (on, config) => {
require('../../../../plugin')(on, config)
// do not instrument the spec files
// on('file:preprocessor', require('../../../../use-babelrc'))
return config
}
2 changes: 2 additions & 0 deletions examples/no-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/no-files/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./commands')
13 changes: 13 additions & 0 deletions examples/no-files/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<body>
Page body
<script src="main.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')
}
</script>
</body>
3 changes: 3 additions & 0 deletions examples/no-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
16 changes: 16 additions & 0 deletions examples/no-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "example-filter-files",
"description": "Filtering out files from the code coverage",
"devDependencies": {
"@babel/core": "7.9.0"
},
"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",
"clean": "rm -rf .nyc_output coverage",
"e2e": "../../node_modules/.bin/start-test 1234 cy:run",
"pree2e": "npm run clean"
}
}
13 changes: 11 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const { getNycReportFilename } = require('./task-utils')
const { existsSync } = require('fs')
const NYC = require('nyc')

const nyc = new NYC({
cwd: process.cwd(),
reporter: 'text',
})

const nycFilename = getNycReportFilename(process.cwd())

function registerCodeCoveragePlugin(on, config) {
require('./task')(on, config)

Expand All @@ -18,8 +23,12 @@ function registerCodeCoveragePlugin(on, config) {

if (reportAfterEachSpec) {
on('after:spec', (t) => {
console.log('after spec %s', t.relative)
return nyc.report()
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)
}
})
}

Expand Down
1 change: 1 addition & 0 deletions support-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="cypress" />
// @ts-check
// helper functions that are safe to use in the browser
// from support.js file - no file system access
Expand Down
39 changes: 39 additions & 0 deletions task-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,43 @@ function readNycOptions(workingDirectory) {
return nycOptions
}

function getNycOptions(workingDirectory) {
if (!workingDirectory) {
workingDirectory = process.cwd()
}

// https://github.com/istanbuljs/nyc#common-configuration-options
const nycReportOptions = readNycOptions(workingDirectory)

if (nycReportOptions.exclude && !Array.isArray(nycReportOptions.exclude)) {
console.error('NYC options: %o', nycReportOptions)
throw new Error('Expected "exclude" to by an array')
}

if (nycReportOptions['temp-dir']) {
nycReportOptions['temp-dir'] = resolve(nycReportOptions['temp-dir'])
} else {
nycReportOptions['temp-dir'] = join(workingDirectory, '.nyc_output')
}

nycReportOptions.tempDir = nycReportOptions['temp-dir']

if (nycReportOptions['report-dir']) {
nycReportOptions['report-dir'] = resolve(nycReportOptions['report-dir'])
}
// seems nyc API really is using camel cased version
nycReportOptions.reportDir = nycReportOptions['report-dir']

return nycReportOptions
}

function getNycReportFilename(workingDirectory) {
const nycReportOptions = getNycOptions(workingDirectory)

const nycFilename = join(nycReportOptions['temp-dir'], 'out.json')
return nycFilename
}

function checkAllPathsNotFound(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))

Expand Down Expand Up @@ -411,5 +448,7 @@ module.exports = {
checkAllPathsNotFound,
tryFindingLocalFiles,
readNycOptions,
getNycOptions,
getNycReportFilename,
includeAllFiles,
}
30 changes: 3 additions & 27 deletions task.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// @ts-check
const istanbul = require('istanbul-lib-coverage')
const { join, resolve } = require('path')
const { join } = require('path')
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
const execa = require('execa')
const {
showNycInfo,
resolveRelativePaths,
checkAllPathsNotFound,
tryFindingLocalFiles,
readNycOptions,
getNycOptions,
includeAllFiles,
} = require('./task-utils')
const { fixSourcePaths } = require('./support-utils')
Expand All @@ -31,31 +31,7 @@ const scripts = pkg.scripts || {}
const DEFAULT_CUSTOM_COVERAGE_SCRIPT_NAME = 'coverage:report'
const customNycReportScript = scripts[DEFAULT_CUSTOM_COVERAGE_SCRIPT_NAME]

const nycReportOptions = (function getNycOption() {
// https://github.com/istanbuljs/nyc#common-configuration-options
const nycReportOptions = readNycOptions(processWorkingDirectory)

if (nycReportOptions.exclude && !Array.isArray(nycReportOptions.exclude)) {
console.error('NYC options: %o', nycReportOptions)
throw new Error('Expected "exclude" to by an array')
}

if (nycReportOptions['temp-dir']) {
nycReportOptions['temp-dir'] = resolve(nycReportOptions['temp-dir'])
} else {
nycReportOptions['temp-dir'] = join(processWorkingDirectory, '.nyc_output')
}

nycReportOptions.tempDir = nycReportOptions['temp-dir']

if (nycReportOptions['report-dir']) {
nycReportOptions['report-dir'] = resolve(nycReportOptions['report-dir'])
}
// seems nyc API really is using camel cased version
nycReportOptions.reportDir = nycReportOptions['report-dir']

return nycReportOptions
})()
const nycReportOptions = getNycOptions(processWorkingDirectory)

const nycFilename = join(nycReportOptions['temp-dir'], 'out.json')

Expand Down

0 comments on commit c0424bd

Please sign in to comment.