Introduction to Testing with Cypress

Here's a quick introduction to Cypress and the reasons why we should use it.

Introduction

Testing JavaScript code is notoriously difficult. Because JavaScript typically runs in a browser environment and interacts closely with the DOM and user behavior, it is often challenging to write valuable tests that are also consistent and fast.

When testing JavaScript, it’s helpful to think about what we want to get out of the testing. In this chapter, we’ll focus on two goals:

  • Using tests to validate that the code is doing what we think it is

  • Using tests to help development by allowing us to rapidly run situations that would take a long time to recreate manually

While a wide variety of testing tools are available in JavaScript, in this chapter our focus is on end-to-end testing with Cypress, a tool that simulates interactions with our Rails server and the browser.

Why focus on just one tool? The various tools overlap in functionality but differ in terminology just enough that discussing more than one of them at a time would be confusing.

Why end-to-end testing? The Stimulus team makes it clear that end-to-end server testing is the preferred way of testing Stimulus behavior. While it’s theoretically possible to test Stimulus controllers or React components in isolation from a server, it’s not recommended for Stimulus, and the React process is also rather complex.

Get hands-on with 1200+ tech skills courses.