diff --git a/.circleci/config.yml b/.circleci/config.yml index 1969760b..e1ff78cc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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: @@ -579,4 +609,5 @@ workflows: - example-use-webpack - example-all-files - example-placeholders + - example-unit-tests-js - Windows test diff --git a/README.md b/README.md index c533f031..661a2ee8 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/examples/unit-tests-js/.babelrc b/examples/unit-tests-js/.babelrc new file mode 100644 index 00000000..7a016cf8 --- /dev/null +++ b/examples/unit-tests-js/.babelrc @@ -0,0 +1,3 @@ +{ + "plugins": ["istanbul"] +} diff --git a/examples/unit-tests-js/README.md b/examples/unit-tests-js/README.md new file mode 100644 index 00000000..0c524884 --- /dev/null +++ b/examples/unit-tests-js/README.md @@ -0,0 +1,3 @@ +# example: unit-tests-js + +Examples that only run unit tests written using JavaScript diff --git a/examples/unit-tests-js/cypress.json b/examples/unit-tests-js/cypress.json new file mode 100644 index 00000000..4ec54f37 --- /dev/null +++ b/examples/unit-tests-js/cypress.json @@ -0,0 +1,3 @@ +{ + "fixturesFolder": false +} diff --git a/examples/unit-tests-js/cypress/integration/spec.js b/examples/unit-tests-js/cypress/integration/spec.js new file mode 100644 index 00000000..d109869d --- /dev/null +++ b/examples/unit-tests-js/cypress/integration/spec.js @@ -0,0 +1,13 @@ +/// +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 + }) +}) diff --git a/examples/unit-tests-js/cypress/plugins/index.js b/examples/unit-tests-js/cypress/plugins/index.js new file mode 100644 index 00000000..b17c48db --- /dev/null +++ b/examples/unit-tests-js/cypress/plugins/index.js @@ -0,0 +1,5 @@ +module.exports = (on, config) => { + require('../../../../task')(on, config) + on('file:preprocessor', require('../../../../use-babelrc')) + return config +} diff --git a/examples/unit-tests-js/cypress/support/index.js b/examples/unit-tests-js/cypress/support/index.js new file mode 100644 index 00000000..dd60efa2 --- /dev/null +++ b/examples/unit-tests-js/cypress/support/index.js @@ -0,0 +1 @@ +import '../../../../support' diff --git a/examples/unit-tests-js/package.json b/examples/unit-tests-js/package.json new file mode 100644 index 00000000..337336be --- /dev/null +++ b/examples/unit-tests-js/package.json @@ -0,0 +1,7 @@ +{ + "name": "example-unit-tests-js", + "description": "Run unit tests written using JavaScript", + "scripts": { + "cy:open": "../../node_modules/.bin/cypress open" + } +} diff --git a/examples/unit-tests-js/src/utils/misc.js b/examples/unit-tests-js/src/utils/misc.js new file mode 100644 index 00000000..c5985c54 --- /dev/null +++ b/examples/unit-tests-js/src/utils/misc.js @@ -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