All Projects → bahmutov → Cypress And Jest

bahmutov / Cypress And Jest

Cypress and Jest both with code coverage running unit tests

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cypress And Jest

Istanbuljs
monorepo containing the various nuts and bolts that facilitate istanbul.js test instrumentation
Stars: ✭ 656 (+433.33%)
Mutual labels:  jest, code-coverage
Sketchmine
Tools to validate, generate and analyse sketch files from web pages
Stars: ✭ 114 (-7.32%)
Mutual labels:  jest
Consolemock
A tool for testing console logs
Stars: ✭ 103 (-16.26%)
Mutual labels:  jest
Jest Extended
Additional Jest matchers 🃏💪
Stars: ✭ 1,763 (+1333.33%)
Mutual labels:  jest
Ts React Boilerplate
Universal React App with Redux 4, Typescript 3, and Webpack 4
Stars: ✭ 104 (-15.45%)
Mutual labels:  jest
Vlackjack
Blackjack built with Vue.js and vuex
Stars: ✭ 109 (-11.38%)
Mutual labels:  jest
Esbuild Jest
A Jest transformer using esbuild
Stars: ✭ 100 (-18.7%)
Mutual labels:  jest
Coinbase Pro Node
Coinbase Pro API written in TypeScript and covered by tests.
Stars: ✭ 116 (-5.69%)
Mutual labels:  code-coverage
Bashcov
Code coverage tool for Bash
Stars: ✭ 113 (-8.13%)
Mutual labels:  code-coverage
What The Splash
Tutorial for building an unsplash image gallery with redux saga :atom:
Stars: ✭ 107 (-13.01%)
Mutual labels:  jest
React Nativeish
React Native / React Native Web Boilerplate
Stars: ✭ 106 (-13.82%)
Mutual labels:  jest
Node Flowtype Boilerplate
This boilerplate repository is outdated and no longer maintained. Instead, I strongly recommend to use TypeScript.
Stars: ✭ 104 (-15.45%)
Mutual labels:  jest
Gest
👨‍💻 A sensible GraphQL testing tool - test your GraphQL schema locally and in the cloud
Stars: ✭ 109 (-11.38%)
Mutual labels:  jest
React Bootstrap Webpack Starter
ReactJS 16.4 + new React Context API +react Router 4 + webpack 4 + babel 7+ hot Reload + Bootstrap 4 + styled-components
Stars: ✭ 103 (-16.26%)
Mutual labels:  jest
Reeakt
A modern React boilerplate to awesome web applications
Stars: ✭ 116 (-5.69%)
Mutual labels:  jest
Hapi Starter Kit
Hapi.js based REST boilerplate which uses latest ES7/ES8 features (async/await) with code coverage and follows best pratices
Stars: ✭ 103 (-16.26%)
Mutual labels:  code-coverage
Auto Spies
Create automatic spies from classes
Stars: ✭ 106 (-13.82%)
Mutual labels:  jest
Cooky Cutter
🍪 Object factories for testing in TypeScript
Stars: ✭ 109 (-11.38%)
Mutual labels:  jest
Typescript Snapshots Plugin
Snapshots language service support for Typescript
Stars: ✭ 122 (-0.81%)
Mutual labels:  jest
Setup Php
GitHub action to set up PHP with extensions, php.ini configuration, coverage drivers, and various tools.
Stars: ✭ 1,945 (+1481.3%)
Mutual labels:  code-coverage

cypress-and-jest renovate-app badge CircleCI

Demo unit tests with code coverage from both Cypress and Jest

Use

# install and run tests
$ npm it
# runs Jest and Cypress tests headlessly
# generates combined code coverage report
$ open coverage/lcov-report/index.html

Merged report

The application source code is just a single file src/calc.js. Let's cover this file with unit tests by using two test runners: Jest and Cypress.

Jest init

The Jest setting were initialized using npx jest --init command:

Jest init

In the generated jest.config.js enable code coverage collection, and output into folder jest-coverage (to avoid clashing with Cypress coverage report).

// jest.config.js
module.exports = {
  // Indicates whether the coverage information should be collected while executing the test
  collectCoverage: true,
  // The directory where Jest should output its coverage files
  coverageDirectory: 'jest-coverage',
  // The test environment that will be used for testing
  testEnvironment: 'node'
}

The Jest unit tests are in the file tests/calc.test.js and the tests only run the add function. We can run the Jest tests and see the coverage summary.

Jest test

The coverage reports in jest-coverage folder by default include JSON, LCOV and static HTML reports. The HTML report shows that the function sub was not reached by the Jest tests.

Jest coverage

Cypress init

First install Cypress

$ npm install -D cypress

Initialize Cypress folder with npx @bahmutov/cly init command.

Cypress init

In cypress/integration/spec.js let's require sub function and test it

// cypress/integration/spec.js
const { sub } = require('../../src/calc')
it('subtracts 10 - 5', () => {
  expect(sub(10, 5)).to.equal(5)
})

The test passes

Cypress test

Cypress code coverage setup

Code coverage in Cypress is done via @cypress/code-coverage plugin. I suggest following the installation instructions in that repo.

First, install the plugin and babel-plugin-istanbul to instrument code.

npm i -D @cypress/code-coverage babel-plugin-istanbul

Add to your cypress/support/index.js file:

import '@cypress/code-coverage/support'

Register tasks in your cypress/plugins/index.js file:

module.exports = (on, config) => {
  require('@cypress/code-coverage/task')(on, config)
  on('file:preprocessor', require('@cypress/code-coverage/use-browserify-istanbul'))
  return config
}

Because we saved the Jest coverage report in jest-coverage, set Cypress to save its coverage to cypress-coverage. Since nyc is used to generate the report, add nyc object to package.json file.

{
  "nyc": {
    "report-dir": "cypress-coverage"
  }
}

Run Cypress with npx cypress open and a report should be saved. As you can see, we have missed the add function!

Cypress coverage

Merge coverage reports

We have two folders with coverage reports generated by Jest and Cypress.

$ ls -la *-coverage
cypress-coverage:
total 24
drwxr-xr-x   6 gleb  staff   204 Jul 22 23:04 .
drwxr-xr-x  20 gleb  staff   680 Jul 22 23:05 ..
-rw-r--r--   1 gleb  staff   744 Jul 22 23:05 clover.xml
-rw-r--r--   1 gleb  staff  1000 Jul 22 23:05 coverage-final.json
drwxr-xr-x  10 gleb  staff   340 Jul 22 23:04 lcov-report
-rw-r--r--   1 gleb  staff   201 Jul 22 23:05 lcov.info

jest-coverage:
total 24
drwxr-xr-x   6 gleb  staff   204 Jul 22 21:00 .
drwxr-xr-x  20 gleb  staff   680 Jul 22 23:05 ..
-rw-r--r--   1 gleb  staff   744 Jul 22 23:05 clover.xml
-rw-r--r--   1 gleb  staff  1000 Jul 22 23:05 coverage-final.json
drwxr-xr-x  10 gleb  staff   340 Jul 22 21:00 lcov-report
-rw-r--r--   1 gleb  staff   201 Jul 22 23:05 lcov.info

Let's combine the two reports and generate the final report. There is a script in package.json that does just that. It copies cypress-coverage/coverage-final.json and jest-coverage/coverage-final.json into a folder, runs nyc merge command, the creates the combined report using nyc report ... command.

$ npm run report:combined
...

coverage files in reports merged into coverage.json

> [email protected] report:combined /Users/gleb/git/cypress-and-jest
> nyc report --reporter lcov --report-dir coverage

The final HTML report shows that we have reached all source statements in calc.js file.

Merged report

Coverage CI artifact

You can store the produced static HTML report on your continuous integration server. For example see .circleci/config.yml file.

Report generated and stored on CircleCI

More info

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].