|
| 1 | +> A little helper to unit test React components in the open source [Cypress.io](https://www.cypress.io/) test runner **v7.0.0+** |
| 2 | +
|
| 3 | +**Jump to:** [Comparison](#comparison), [Blog posts](#blog-posts), [Install](#install), Examples: [basic](#basic-examples), [advanced](#advanced-examples), [full](#full-examples), [external](#external-examples), [Style options](#options), [Code coverage](#code-coverage), [Visual testing](#visual-testing), [Common problems](#common-problems), [Chat](#chat) |
| 4 | + |
| 5 | +## TLDR |
| 6 | + |
| 7 | +- What is this? This package allows you to use [Cypress](https://www.cypress.io/) test runner to unit test your Angular components with zero effort. Here is a typical component testing, notice there is not external URL shown, since it is mounting the component directly. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +- How is this different from [Angular Testing](https://angular.io/guide/testing) or [ATL](https://testing-library.com/docs/angular-testing-library/intro/)? It is similar in functionality BUT runs the component in the real browser with full power of Cypress E2E test runner: [live GUI, full API, screen recording, CI support, cross-platform](https://www.cypress.io/features/), and [visual testing](https://on.cypress.io/visual-testing). |
| 12 | +- Read [My Vision for Component Tests in Cypress](https://glebbahmutov.com/blog/my-vision-for-component-tests/) by Gleb Bahmutov |
| 13 | + |
| 14 | +## Comparison |
| 15 | + |
| 16 | +<!-- prettier-ignore-start --> |
| 17 | +Feature | Jest / Karma / ATL | Cypress + `@cypress/angular` |
| 18 | +--- | --- | --- |
| 19 | +Test runs in real browser | ❌ | ✅ |
| 20 | +Supports shallow mount | ✅ | ❌ |
| 21 | +Supports full mount | ✅ | ✅ |
| 22 | +Test speed | 🏎 | [as fast as the app works in the browser](#fast-enough) |
| 23 | +Test can use additional plugins | maybe | use any [Cypress plugin](https://on.cypress.io/plugins) |
| 24 | +Test can interact with component | synthetic limited API | use any [Cypress command](https://on.cypress.io/api) |
| 25 | +Test can be debugged | via terminal and Node debugger | use browser DevTools |
| 26 | +Built-in time traveling debugger | ❌ | Cypress time traveling debugger |
| 27 | +Re-run tests on file or test change | ✅ | ✅ |
| 28 | +Test output on CI | terminal | terminal, screenshots, videos |
| 29 | +Tests can be run in parallel | ✅ | ✅ via [parallelization](https://on.cypress.io/parallelization) |
| 30 | +Test against interface | if using `@testing-library/angular` | ✅ and can use `@testing-library/cypress` |
| 31 | +Spying and stubbing methods | Jest mocks | [Sinon library](https://on.cypress.io/stubs-spies-and-clocks) |
| 32 | +Stubbing imports | ✅ | ✅ |
| 33 | +Stubbing clock | ✅ | ✅ |
| 34 | +Code coverage | ✅ | ✅ |
| 35 | +<!-- prettier-ignore-end --> |
| 36 | + |
| 37 | +If you are coming from Jest + ATL world, read [Test The Interface Not The Implementation](https://glebbahmutov.com/blog/test-the-interface/). |
| 38 | + |
| 39 | +## Blog posts |
| 40 | + |
| 41 | +- [My Vision for Component Tests in Cypress](https://glebbahmutov.com/blog/my-vision-for-component-tests/) |
| 42 | + |
| 43 | +## Install |
| 44 | + |
| 45 | +Requires [Node](https://nodejs.org/en/) version 12 or above. |
| 46 | + |
| 47 | +```sh |
| 48 | +npm install --save-dev cypress @cypress/angular @cypress/webpack-dev-server |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +## API |
| 53 | + |
| 54 | +- `mount` allows you to mount a given Angular component as a mini web application and interact with it using Cypress commands |
| 55 | + |
| 56 | +## Examples |
| 57 | + |
| 58 | +```ts |
| 59 | +import { mount } from '@cypress/angular' |
| 60 | +import { HelloWorldComponent } from './hello-world.component' |
| 61 | + |
| 62 | +describe('HelloWorldComponent', () => { |
| 63 | + it('works', () => { |
| 64 | + mount(HelloWorldComponent) |
| 65 | + // now use standard Cypress commands |
| 66 | + cy.contains('Hello World!').should('be.visible') |
| 67 | + }) |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +Look at the examples in [cypress/component](cypress/component) folder. Here is the list of examples showing various testing scenarios. |
| 72 | + |
| 73 | +### Basic examples |
| 74 | +Coming Soon... |
| 75 | + |
| 76 | + |
| 77 | +### Advanced examples |
| 78 | +Coming Soon... |
| 79 | + |
| 80 | +### Full examples |
| 81 | +Coming Soon... |
| 82 | + |
| 83 | +### External examples |
| 84 | +Coming Soon... |
| 85 | + |
| 86 | +## Options |
| 87 | + |
| 88 | + |
| 89 | +## Code coverage |
| 90 | + |
| 91 | +In order to use code coverage you can follow the instructions from [docs](https://github.com/cypress-io/code-coverage). In most of cases you need to install 2 dependencies: |
| 92 | + |
| 93 | +``` |
| 94 | +npm i @cypress/code-coverage babel-plugin-istanbul |
| 95 | +
|
| 96 | +yarn add @cypress/code-coverage babel-plugin-istanbul |
| 97 | +``` |
| 98 | + |
| 99 | +If you are using [plugins/cra-v3](plugins/cra-v3) it instruments the code on the fly using `babel-plugin-istanbul` and generates report using dependency [cypress-io/code-coverage](https://github.com/cypress-io/code-coverage) (included). If you want to disable code coverage instrumentation and reporting, use `--env coverage=false` or `CYPRESS_coverage=false` or set in your `cypress.json` file |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "env": { |
| 104 | + "coverage": false |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Visual testing |
| 110 | + |
| 111 | +You can use any Cypress [Visual Testing plugin](https://on.cypress.io/plugins#visual-testing) to perform [visual testing](https://on.cypress.io/visual-testing) from the component tests. This repo has several example projects, see [visual-sudoku](examples/visual-sudoku), [visual-testing-with-percy](examples/visual-testing-with-percy), [visual-testing-with-happo](examples/visual-testing-with-happo), and [visual-testing-with-applitools](examples/visual-testing-with-applitools). |
| 112 | + |
| 113 | +For a larger Do-It-Yourself example with an hour long list of explanation videos, see [bahmutov/sudoku](https://github.com/bahmutov/sudoku) repository. I explain how to write visual testing using open source tools in this [blog post](https://glebbahmutov.com/blog/open-source-visual-testing-of-components/), [video talk](https://www.youtube.com/watch?v=00BNExlJUU8), and [slides](https://slides.com/bahmutov/i-see-what-is-going-on). |
| 114 | + |
| 115 | +## Common problems |
| 116 | + |
| 117 | + |
| 118 | +## Chat |
| 119 | + |
| 120 | +Come chat with us [on discord](https://discord.gg/7ZHYhZSW) in the #component-testing channel. |
| 121 | + |
| 122 | +## Development |
| 123 | + |
| 124 | +See [docs/development.md](./docs/development.md) |
| 125 | + |
| 126 | +## Debugging |
| 127 | + |
| 128 | +You can see verbose logs from this plugin by running with environment variable |
| 129 | + |
| 130 | +``` |
| 131 | +DEBUG=@cypress/angular |
| 132 | +``` |
| 133 | + |
| 134 | +Because finding and modifying Webpack settings while running this plugin is done by [find-webpack](https://github.com/bahmutov/find-webpack) module, you might want to enable its debug messages too. |
| 135 | + |
| 136 | +``` |
| 137 | +DEBUG=@cypress/angular,find-webpack |
| 138 | +``` |
| 139 | + |
| 140 | +## Changelog |
| 141 | + |
| 142 | +[Changelog](./CHANGELOG.md) |
| 143 | + |
| 144 | +## Related tools |
| 145 | + |
| 146 | +Same feature for unit testing components from other frameworks using Cypress |
| 147 | + |
| 148 | +- [@cypress/react](https://github.com/cypress-io/cypress/tree/develop/npm/react) |
| 149 | +- [@cypress/vue](https://github.com/cypress-io/cypress/tree/develop/npm/vue) |
| 150 | +- [cypress-cycle-unit-test](https://github.com/bahmutov/cypress-cycle-unit-test) |
| 151 | +- [cypress-svelte-unit-test](https://github.com/bahmutov/cypress-svelte-unit-test) |
| 152 | +- [@cypress/angular](https://github.com/bahmutov/@cypress/angular) |
| 153 | +- [cypress-hyperapp-unit-test](https://github.com/bahmutov/cypress-hyperapp-unit-test) |
| 154 | +- [cypress-angularjs-unit-test](https://github.com/bahmutov/cypress-angularjs-unit-test) |
0 commit comments