Skip to content

add example with just unit test code coverage #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,36 @@ workflows:
../../node_modules/.bin/only-covered main.js
working_directory: examples/one-spec

- cypress/run:
attach-workspace: true
name: example-unit-tests-js
requires:
- cypress/install
# there are no jobs to follow this one
# so no need to save the workspace files (saves time)
no-workspace: true
command: npx cypress run --project examples/unit-tests-js
# store screenshots and videos
store_artifacts: true
post-steps:
- run: cat examples/unit-tests-js/.nyc_output/out.json
- run: cat examples/unit-tests-js/coverage/coverage-summary.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/unit-tests-js/coverage
# make sure the examples captures 100% of code
- run:
command: npx nyc report --check-coverage true --lines 100
working_directory: examples/unit-tests-js
- run:
name: Check code coverage 📈
command: |
../../node_modules/.bin/check-coverage misc.js
../../node_modules/.bin/only-covered misc.js
working_directory: examples/unit-tests-js

- cypress/run:
name: Windows test
executor:
Expand Down Expand Up @@ -579,4 +609,5 @@ workflows:
- example-use-webpack
- example-all-files
- example-placeholders
- example-unit-tests-js
- Windows test
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ module.exports = (on, config) => {

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

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).

### Alternative for unit tests

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.
Expand Down Expand Up @@ -401,6 +403,7 @@ Full examples we use for testing in this repository:
- [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
- [examples/ts-example](examples/ts-example) uses Babel + Parcel to instrument and serve TypeScript file
- [examples/use-webpack](examples/use-webpack) shows Webpack build with source maps and Babel
- [examples/unit-tests-js](examples/unit-tests-js) runs just the unit tests and reports code coverage (JavaScript source code)

### External examples

Expand Down
3 changes: 3 additions & 0 deletions examples/unit-tests-js/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["istanbul"]
}
3 changes: 3 additions & 0 deletions examples/unit-tests-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# example: unit-tests-js

Examples that only run unit tests written using JavaScript
3 changes: 3 additions & 0 deletions examples/unit-tests-js/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"fixturesFolder": false
}
13 changes: 13 additions & 0 deletions examples/unit-tests-js/cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference types="cypress" />
import { isObject, add } from '../../src/utils/misc'

describe('unit tests', () => {
it('adds two numbers', () => {
expect(add(2, 3)).to.equal(5)
})

it('checks for object', () => {
expect(isObject({}), '{}').to.be.true
expect(isObject([]), '[]').to.be.true
})
})
5 changes: 5 additions & 0 deletions examples/unit-tests-js/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
}
1 change: 1 addition & 0 deletions examples/unit-tests-js/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../../../support'
7 changes: 7 additions & 0 deletions examples/unit-tests-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example-unit-tests-js",
"description": "Run unit tests written using JavaScript",
"scripts": {
"cy:open": "../../node_modules/.bin/cypress open"
}
}
13 changes: 13 additions & 0 deletions examples/unit-tests-js/src/utils/misc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Very simple check, returns true for arrays as well
*/
export function isObject(object) {
return object != null && typeof object === 'object'
}

/**
* Adds two numbers together
* @param {number} a
* @param {number} b
*/
export const add = (a, b) => a + b