Hey, this is Gleb Bakhmutov from Cypress.io. Thank you for inviting me. I want to talk about common mistakes people make when writing Cypress tests. And first, I want to remind that we are still in climate crisis. Despite COVID slowdown, we have to act and act now. You can change your life or better join an organization that fights together. You can join multiple organizations. I recommend both here.
In this presentation, I'll cover common mistakes in Cypress tests. And then I'll talk about something we're trying to do to minimize the number of mistakes by improving our documentation. I'll finish with a discussion of our github repository. You can find the slides online and if you have questions, please find me on Twitter.
Let's start with Cypress mistakes. So when people start writing Cypress tests, we forget that Cypress tests run in the browser. I know it's a simple fact, but it's easy to write something like this, require file system module and then try to read the file. Well, this will never work because the Cypress test executes in the browser. You would not be able to execute this code in your application browser code, right? So instead of trying to access the file system and operating system directly from your spec, you want to go through the Cypress command that we provide to access the file system, node code and your operating system. You can read file, write file, execute any program or execute node code using task.
The task is the most powerful command that's one that executes the node code that you write. For example, a very common use case is try to connect to a database. For example, if you want to reset the database before the start of a test. If you write your plugin file like this, it runs in node, you can reuse part of your application code to connect to the database and then, for example, truncate the table on a task. We have very good examples of truncating the database and reseeding it with data in our Cypress real-world application. You can see that we are executing the task DB seed before each test.
One little detail that I want to point out, if you look at the spec file names, well, here's an example of API test and here's an example of a user interface test. A very common mistake is not picking the right type of a test. If your test is hard to write, hard to maintain, has a lot of boilerplate, well, maybe you picked a wrong type of a test and are struggling against magrane. If you're testing a user flow for web application, it's an end-to-end test. If you're testing an individual piece of code like a function or a class, you probably want to write a unit test. If you're trying to test a server and how it responds to a REST or a GraphQL request, you want to write an API test.
Comments