Skip to content

Commit 4d3bf42

Browse files
authored
add example with just unit test code coverage (#384)
1 parent 89d4ab5 commit 4d3bf42

File tree

10 files changed

+82
-0
lines changed

10 files changed

+82
-0
lines changed

.circleci/config.yml

+31
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,36 @@ workflows:
520520
../../node_modules/.bin/only-covered main.js
521521
working_directory: examples/one-spec
522522

523+
- cypress/run:
524+
attach-workspace: true
525+
name: example-unit-tests-js
526+
requires:
527+
- cypress/install
528+
# there are no jobs to follow this one
529+
# so no need to save the workspace files (saves time)
530+
no-workspace: true
531+
command: npx cypress run --project examples/unit-tests-js
532+
# store screenshots and videos
533+
store_artifacts: true
534+
post-steps:
535+
- run: cat examples/unit-tests-js/.nyc_output/out.json
536+
- run: cat examples/unit-tests-js/coverage/coverage-summary.json
537+
# store the created coverage report folder
538+
# you can click on it in the CircleCI UI
539+
# to see live static HTML site
540+
- store_artifacts:
541+
path: examples/unit-tests-js/coverage
542+
# make sure the examples captures 100% of code
543+
- run:
544+
command: npx nyc report --check-coverage true --lines 100
545+
working_directory: examples/unit-tests-js
546+
- run:
547+
name: Check code coverage 📈
548+
command: |
549+
../../node_modules/.bin/check-coverage misc.js
550+
../../node_modules/.bin/only-covered misc.js
551+
working_directory: examples/unit-tests-js
552+
523553
- cypress/run:
524554
name: Windows test
525555
executor:
@@ -579,4 +609,5 @@ workflows:
579609
- example-use-webpack
580610
- example-all-files
581611
- example-placeholders
612+
- example-unit-tests-js
582613
- Windows test

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ module.exports = (on, config) => {
136136

137137
Now the code coverage from spec files will be combined with end-to-end coverage.
138138

139+
Find example of a just the unit tests and JavaScript source files with collected code coverage in [examples/unit-tests-js](./examples/unit-tests-js).
140+
139141
### Alternative for unit tests
140142

141143
If you cannot use `.babelrc` for some reason (maybe it is used by other tools?), try using the Browserify transformer included with this module in `use-browserify-istanbul` file.
@@ -401,6 +403,7 @@ Full examples we use for testing in this repository:
401403
- [examples/one-spec.js](examples/one-spec.js) confirms that coverage is collected and filtered correctly if the user only executes a single Cypress test
402404
- [examples/ts-example](examples/ts-example) uses Babel + Parcel to instrument and serve TypeScript file
403405
- [examples/use-webpack](examples/use-webpack) shows Webpack build with source maps and Babel
406+
- [examples/unit-tests-js](examples/unit-tests-js) runs just the unit tests and reports code coverage (JavaScript source code)
404407

405408
### External examples
406409

examples/unit-tests-js/.babelrc

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

examples/unit-tests-js/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# example: unit-tests-js
2+
3+
Examples that only run unit tests written using JavaScript

examples/unit-tests-js/cypress.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"fixturesFolder": false
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference types="cypress" />
2+
import { isObject, add } from '../../src/utils/misc'
3+
4+
describe('unit tests', () => {
5+
it('adds two numbers', () => {
6+
expect(add(2, 3)).to.equal(5)
7+
})
8+
9+
it('checks for object', () => {
10+
expect(isObject({}), '{}').to.be.true
11+
expect(isObject([]), '[]').to.be.true
12+
})
13+
})
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 @@
1+
import '../../../../support'

examples/unit-tests-js/package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "example-unit-tests-js",
3+
"description": "Run unit tests written using JavaScript",
4+
"scripts": {
5+
"cy:open": "../../node_modules/.bin/cypress open"
6+
}
7+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Very simple check, returns true for arrays as well
3+
*/
4+
export function isObject(object) {
5+
return object != null && typeof object === 'object'
6+
}
7+
8+
/**
9+
* Adds two numbers together
10+
* @param {number} a
11+
* @param {number} b
12+
*/
13+
export const add = (a, b) => a + b

0 commit comments

Comments
 (0)