An open API service indexing awesome lists of open source software.

https://github.com/bahmutov/try-cra-app-typescript

An example of Create-React-App v3 using TypeScript with Cypress unit tests
https://github.com/bahmutov/try-cra-app-typescript

cypress-react-unit-test-example

Last synced: 4 months ago
JSON representation

An example of Create-React-App v3 using TypeScript with Cypress unit tests

Awesome Lists containing this project

README

        

# try-cra-app-typescript
[![ci status][ci image]][ci url] ![cypress version](https://img.shields.io/badge/cypress-6.1.0-brightgreen) ![@cypress/react version](https://img.shields.io/badge/@cypress/react-4.16.4-brightgreen) ![check-code-coverage](https://img.shields.io/badge/code--coverage-100%25-brightgreen)

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app) using TypeScript template

```shell
$ npx create-react-app try-cra-app-typescript --template typescript
info Direct dependencies
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]
```

Then add Cypress and [cypress-react-unit-test](https://github.com/bahmutov/cypress-react-unit-test)

```shell
$ yarn add -D cypress cypress-react-unit-test
info Direct dependencies
├─ [email protected]
└─ [email protected]
```

Create [cypress.json](cypress.json) and point at the unit testing settings: support and plugins to read the create-react-app webpack config and load tests from [src](src) folder.

Create [src/App.cy-spec.tsx](src/App.cy-spec.tsx). Run using `yarn cypress open` and you should see the working component test.

![Working unit test](images/works.png)

The test vs the original [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) test file (nothing like type clashes between Mocha and Jest assertions, right)

![Code](images/initial-code.png)

## Code coverage

The component tests collect code coverage automatically. You should see messages in the command log.

![Code coverage messages](images/messages.png)

When running on CI, the collected coverage information is checked using [check-code-coverage](https://github.com/bahmutov/check-code-coverage) utility. If the code coverage is not being collected, run Cypress with environment variable to see logs:

```
DEBUG=cypress-react-unit-test,find-webpack
```

[ci image]: https://github.com/bahmutov/try-cra-app-typescript/workflows/master/badge.svg?branch=master
[ci url]: https://github.com/bahmutov/try-cra-app-typescript/actions