Giter VIP home page Giter VIP logo

testing-react-with-jest-enzyme-and-cypress's Introduction

Testing React with Jest, Enzyme and Cypress

code style: prettier Cypress.io tests

This simple application has been created as an exercise on my way to learn and master testing in JavaScript and React. The testing libraries that have been chosen are Jest, Enzyme and Cypress. [Update June 2018 - In my future projects I will switch to react-testing-library in place of Enzyme]. The application has been built using create-react-app, which takes care of setting up the development environment and ships with Jest as default testing library.

Getting Started

To install this application you can (fork and) download it and then run the command npm install from your terminal. After the installation has successfully complete, you can launch it with the command npm start. To run the unit tests, use the command npm test in a separate command prompt window. To run the end-to-end tests (at the moment there's only a basic one) with Cypress, type npx cypress open at the root of the project folder and then Run all specs in the Cypress window that will open.

Built With

  • React - A JavaScript library for building user interfaces
  • Jest - A complete and easy to set-up JavaScript testing solution
  • Enzyme - A JavaScript Testing utility for React
  • Cypress - A test runner for anything that runs in a browser

Author

Notes

The tests to mock a call to an external API are taken from the course Testing React with Jest and Enzyme. The article But really, what is a JavaScript mock? by Kent C. Dodds helped me simplify the mock test even further by using a mock directory. Instead of using the built-in React Router v4 library, I re-wrote the custom version built by Tyler McGinnis in his course (reference in Acknowledgments below).

Notes on the Installation process

It's probably enough to follow the instructions on the Getting Started section above to install and run the application. If you encounter an error about missing the jest-cli module, though, here are some steps to follow.

First error message:

  1. Error: Cannot find module 'jest-cli'.

After reinstalling the 'jest-cli' module, another one would show up:

  1. TypeError: environment.setup is not a function.

The solution suggested on this issue by Dan Abramov (Gaeron) solved the problem. Here is what he wrote: "If you have both react-scripts and jest in your package.json, delete jest from it. Then delete package-lock.json, yarn.lock and node_modules. Then run npm install (or yarn if you use it)."

Notes on Flow, flow-typed and Visual Studio Code

As Visual Studio Code has been used as IDE for this project and there seems to be issues about the extension it uses to support Flow, this project commits regarding Flow have been reverted.
All code regarding Flow has been moved to another branch, while in the master branch it will be only added type checking with PropTypes.

License

This project is licensed under the MIT License.

Acknowledgments

testing-react-with-jest-enzyme-and-cypress's People

Contributors

daniel-234 avatar

Watchers

James Cloos avatar Sharath Kumar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.